我已经使用boost
了一段时间,但仍然是新手。我想我爱上了,boost::any
因为我不再需要使用模板来处理使用或返回的类型化对象。然而,我觉得对我来说仍然不方便的一件事是更平滑的功能或方法,如果有的话,可以held
简洁地来回传输其数据,并且以后肯定不会使用cast_any
.
习俗
(case 1)
boost::any a="123456";
std::string s=boost::cast_any<std::string>(a);
(case 2)
boost:any a=123456;
int i=boost::cast_any<int>a;
自定义 boost_any:
(case 1)
boost:any a="123456";
std::string s=a;
(case 2)
boost:any a=123456;
int i=a;
我喜欢这个功能,因为在调试我的程序期间,我发现这held="123456"
两种情况下的对象完全相同。我think
lib 的内部实现可能已经做到了(将它保存的内容转换为string
)。此示例仅显示 POD 类型,我想知道是否有人对此自定义进行了任何操作或为我消除cast_any
.