为什么 boost::any 不保存字符串文字
Why boost::any does not hold string literal?
#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;
};
注释的代码行无法编译,为什么?我认为字符串文字在大多数情况下可以充当字符*,这是相关的 r 值和 l-rvalue 吗?
错误信息:test_boost_any.cc
D:Program Files (x86)Microsoft Visual Studio 11.0VCINCLUDExlocale(336) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
e:projectsframeworkboost_1_53_0boost/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:projectsframeworkboost_1_53_0boost/any.hpp(139) : see declaration
of 'boost::any::holder<ValueType>::held'
with
[
ValueType=const char [5]
]
e:projectsframeworkboost_1_53_0boost/any.hpp(120) : while compiling
class template member function 'boost::any::holder<ValueType>::holder(ValueType
(&))'
with
[
ValueType=const char [5]
]
e:projectsframeworkboost_1_53_0boost/any.hpp(47) : see reference to
function template instantiation 'boost::any::holder<ValueType>::holder(ValueType
(&))' being compiled
with
[
ValueType=const char [5]
]
e:projectsframeworkboost_1_53_0boost/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]
]
字符串文字不是指针,它是array of N const char
,在您的情况下,因为boost::any
构造函数接收T
(推导为char[5]
,而不是const char*
,数组到指针的转换在这里不起作用),但你不能用initializer-list
中的另一个数组初始化一个数组。
Boost.any 值必须有效才能分配(要求 ValueType
)。但是,字符串文本是一个数组,数组不能在C++中分配。
如果需要,您可以将文本转换为const char *
。
这里 C 中粗糙的数组语义最简单的解决方法是
boost::any charArr = +"haha";
请注意,使用 +
将 char 数组隐式衰减为const char*
其他人已经解释了数组值语义的问题
编译器会告诉你它不能接受数组,例如VS2010会告诉你:
1>D:SRCCDRTrunkDRITThirdPartyboost/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:SRCCDRTrunkDRITThirdPartyboost/any.hpp(139) : see declaration of 'boost::any::holder<ValueType>::held'
1> with
1> [
1> ValueType=const char [5]
1> ]
1> D:SRCCDRTrunkDRITThirdPartyboost/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:SRCCDRTrunkDRITThirdPartyboost/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
(如果有的话)。
下面是在 Boost 中添加阵列支持的讨论链接。
这个问题
的简化版本:
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;
}
相关文章:
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 如何使用字符串文字作为宏参数
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 构造<int>具有 2 个字符串文字的向量
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- 方便地对C++中的所有字符串文字进行模糊处理
- 比较 std::string 和 C 样式字符串文字
- 如何从char16_t字符串文字中读取双精度?
- 如果我在块中编写字符串文字,是否会从数据部分复制整个字符串数据?
- C++ 不能在cout中使用向量和字符串文字
- 为什么必须将 const 添加到 constexpr 中才能进行字符串文字声明?
- C++:Unicode 字符串文字的可移植性
- 返回空字符串文字 VS. 返回空点 - 它们是一样的吗?
- 确保字符指针始终指向相同的字符串文字
- 如何按照 Google C++风格指南连接字符串文字?
- 通过 constexpr 中的 'const char *' glvalue 访问字符串文字的值会出错
- 字符串文字到 char 数组的转换如何在C++中实际工作
- 另一个字符串文字,UDL 迷宫
- strlen 是否针对字符串文字进行了优化?
- 用户定义的字符串文字为字符串null终止