C++ 将动态分配对象传递到boost::any构造函数

C++ 将动态分配对象传递到boost::any构造函数,c++,boost,any,C++,Boost,Any,因此,要正确理解它,正确的程序输出需要: A::A()0x24f5c20 not empty 0 0 ~A::A()0x24f5c20 看来我不明白发生了什么事。 这是用g++版本5.4.0编译的。 人!我怎么了 我假设您正在运行旧版本的程序,或者没有正确地重建它。(我已经使用Boost1.62和1.64进行了测试,使用GCC5.4和Clang。) 该程序具有预期效果,不会调用任何类型的未定义行为 但是请注意,没有理由在此处使用new/delete,并且在初始化boost::any后不使用st

因此,要正确理解它,正确的程序输出需要:

A::A()0x24f5c20
not empty
0
0
~A::A()0x24f5c20
看来我不明白发生了什么事。 这是用g++版本5.4.0编译的。
人!我怎么了

我假设您正在运行旧版本的程序,或者没有正确地重建它。(我已经使用Boost1.62和1.64进行了测试,使用GCC5.4和Clang。)

该程序具有预期效果,不会调用任何类型的未定义行为

但是请注意,没有理由在此处使用
new
/
delete
,并且在初始化
boost::any
后不使用
str
,因为
boost::any
存储值-从不引用。因此,你可以简单地说

A::A()0x24f5c20
A::A(const A&)some address // copy str before passing into any constructor
not empty 
some address 
0 
~A::A()some address //~any() call ~A()some address
~A::A()0x24f5c20
或者只是

auto *str = new Str;
boost::any a(*str);
delete str;
甚至

Str str;
boost::any a(str);

您可以进行比较。我假设您正在运行旧版本的程序,或者没有正确地重建它。(我已经使用Boost1.62和1.64进行了测试,使用GCC5.4和Clang。)

该程序具有预期效果,不会调用任何类型的未定义行为

但是请注意,没有理由在此处使用
new
/
delete
,并且在初始化
boost::any
后不使用
str
,因为
boost::any
存储值-从不引用。因此,你可以简单地说

A::A()0x24f5c20
A::A(const A&)some address // copy str before passing into any constructor
not empty 
some address 
0 
~A::A()some address //~any() call ~A()some address
~A::A()0x24f5c20
或者只是

auto *str = new Str;
boost::any a(*str);
delete str;
甚至

Str str;
boost::any a(str);

你可以比较事物,但我无法重现。你用的是哪个版本的Boost?我用的是1.62版本的Boost。我无法复制。你用的是哪个版本的Boost?我用的是1.62版本的Boost。