placementnew2.cpp
431 Bytes
#include <new>
#include <string>
#include <iostream>
using namespace std;
int
main ()
{
std::aligned_storage_t<sizeof(string), alignof(string)> buf;
string *s = new (&buf) string; // construct an string at .buf;. invokes: operator new(sizeof(string),buf);
// actually undefined behaviour - alignment restrictions might not be fulfilled
*s = "hello";
cout << *s << endl;
s->~string();
};