这是我的代码:
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();
}
}
};
任何有关如何添加正确处理内部分配 std::string 情况的复制构造函数的建议,将不胜感激。