#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 *,这是相关的r值和l-rvalue吗?
错误讯息: 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]
]
答案 0 :(得分:4)
string-literal不是指针,在你的情况下是array of N const char
,因为boost::any
构造函数接收T
(推导到char[5]
,而不是const char*
1}},数组到指针的转换在这里不起作用),但你不能用initializer-list
中的另一个数组初始化数组。
答案 1 :(得分:2)
Boost.any值必须有效才能分配(ValueType
的要求)。但是,字符串文字是一个数组,并且不能在C ++中分配数组。
如果需要,您可以将文字投射到const char *
。
答案 2 :(得分:2)
C中 crufty数组语义的最简单的解决方法是
boost::any charArr = +"haha";
注意使用+
隐式将char数组衰减为const char*
其他人用数组值语义解释了这个问题
答案 3 :(得分:1)
编译器告诉你它不能接受数组,例如VS2010会告诉你:
1>D:\SRC\CDR\Trunk\DRIT\ThirdParty\boost/any.hpp(122): error C2536: 'boost::any::holder<ValueType>::boost::any::holder<ValueType>::held' : cannot specify explicit initializer for arrays
1> with
1> [
1> ValueType=const char [5]
1> ]
1> D:\SRC\CDR\Trunk\DRIT\ThirdParty\boost/any.hpp(139) : see declaration of 'boost::any::holder<ValueType>::held'
1> with
1> [
1> ValueType=const char [5]
1> ]
1> D:\SRC\CDR\Trunk\DRIT\ThirdParty\boost/any.hpp(120) : while compiling class template member function 'boost::any::holder<ValueType>::holder(ValueType (&))'
1> with
1> [
1> ValueType=const char [5]
1> ]
1> D:\SRC\CDR\Trunk\DRIT\ThirdParty\boost/any.hpp(46) : see reference to class template instantiation 'boost::any::holder<ValueType>' being compiled
1> with
1> [
1> ValueType=const char [5]
1> ]
1> toto.cpp(20) : see reference to function template instantiation 'boost::any::any<const char[5]>(ValueType (&))' being compiled
1> with
1> [
1> ValueType=const char [5]
1> ]
“哈哈”的类型不是const char*
而是const char[5]
。
如果将字符串转换为char*
,则会编译:
boost::any charArr = static_cast<const char*>("haha"); // will compile
或者,您可以只存储std::string
。我怀疑这是因为数组不能存储为指针。您也可以使用boost::array
或std::array
(如果有的话)。
Here是在Boost中添加数组支持的讨论的链接。
答案 4 :(得分:0)
此问题的简化版本:
template <typename T>
class Array
{
public:
Array(T value) : value_(value) {}
private:
T value_;
};
int main()
{
int a[5] = {1,2,3,4,5};
Array<int[5]> arr = a;
return 0;
}