#include <boost/any.hpp>
#include <list>
#include <string>
#include <vector>
struct _time_t {
int month;
int year;
};
int main()
{
std::string str = "hahastr";
_time_t t;
std::vector<boost::any> objVec;
objVec.push_back(1);
char* pstr = "haha";
//boost::any charArr = "haha"; not compile
//objVec.push_back("haha"); not compile
objVec.push_back(pstr);
objVec.push_back(str);
objVec.push_back(t);
return 0;
};
注释的代码行无法编译,为什么?我认为在大多数情况下字符串文字可以充当 char*,这与右值和左右值相关吗?
错误信息:
test_boost_any.cc
D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xlocale(336) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
e:\projects\framework\boost_1_53_0\boost/any.hpp(122) : error C2536: 'boost::any
::holder<ValueType>::boost::any::holder<ValueType>::held' : cannot specify expli
cit initializer for arrays
with
[
ValueType=const char [5]
]
e:\projects\framework\boost_1_53_0\boost/any.hpp(139) : see declaration
of 'boost::any::holder<ValueType>::held'
with
[
ValueType=const char [5]
]
e:\projects\framework\boost_1_53_0\boost/any.hpp(120) : while compiling
class template member function 'boost::any::holder<ValueType>::holder(ValueType
(&))'
with
[
ValueType=const char [5]
]
e:\projects\framework\boost_1_53_0\boost/any.hpp(47) : see reference to
function template instantiation 'boost::any::holder<ValueType>::holder(ValueType
(&))' being compiled
with
[
ValueType=const char [5]
]
e:\projects\framework\boost_1_53_0\boost/any.hpp(46) : see reference to
class template instantiation 'boost::any::holder<ValueType>' being compiled
with
[
ValueType=const char [5]
]
test_boost_any.cc(19) : see reference to function template instantiation
'boost::any::any<const char[5]>(ValueType (&))' being compiled
with
[
ValueType=const char [5]
]
string-literal 不是指针,它是array of N const char
,就你而言,因为boost::any
构造函数接收T
(由此推导出char[5]
,不const char*
,数组到指针的转换在这里不起作用),但不能通过数组中的另一个数组来初始化数组initializer-list
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)