Here’s my code:
struct RS_Token
{
char id;
char cleanup;
unsigned char array[sizeof (std::string) > sizeof (double) ? sizeof (std::string) : sizeof (double)];
RS_Token(int a) :
id(a),
cleanup(0)
{
}
RS_Token(int a, const char* pstr) : // identifier or text
id(a),
cleanup(1)
{
new (array) std::basic_string<unsigned char>((unsigned char*)pstr);
}
RS_Token(int a, int b) : // integer
id(a),
cleanup(0)
{
new (array) int(b);
}
RS_Token(int a, double b) : // float (double)
id(a),
cleanup(0)
{
new (array) double(b);
}
~RS_Token()
{
if (cleanup)
{
std::basic_string<unsigned char>* p = reinterpret_cast<std::basic_string<unsigned char>*>(array);
p->~basic_string();
}
}
};
Any suggestions on how to add a copy constructor that properly handles the case where a std::string has been allocated internally, would be appreciated.
I’m not sure that what you’re doing is at all a good design, but to answer your question about placement-new: You provide the constructor arguments just like in any other
newexpression:Construct new string:
Copy-construct:
Assign: