我已经使用了boost :: variant一段时间,现在我正在试图弄清楚它是如何在内部工作的。我写了一个简单的测试,我无法理解结果。这里是(简化)boost ::变体对象构造计数VS销毁计数
struct my_type
{
my_type(){ cout << (size_t)this << " construction"; }
~my_type(){ cout << (size_t)this << " destruction"; }
};
int main()
{
variant<int, my_type> x;
x = my_type();
}
这样的程序的输出是
140736940365327 construction <-- A
140736940365236 destruction <-- ?
140736940365327 destruction <-- A
140736940365332 destruction <-- ?
凭啥析构函数不调用多次构造?由于析构函数是通过堆调用的,我知道这可能不是段错误,但在我看来,这种行为是危险的。我错过了什么吗?这与boost :: variant的“备份”机制有关吗?
向您的课堂添加复制构造函数。 – Mat 2013-02-23 12:22:23