[英]How can I find out if boost::any contains a literal string?
I have this code 我有这个代码
#include <boost/any.hpp>
std::vector<boost::any> a( {1,2,3,"hello",3.0});
for (int i = 0; i < a.size();i++)
{
if (a[i].type() == typeid(int)) // this works
{
std::cout << "int";
}
if (a[i].type() == typeid(char*)) // this does not work I know why
{
std::cout << "char*";
}
}
What if
statement should I use to detect "hello"
, or any sized literal string? 我应该使用
if
语句检测"hello"
或任何大小的文字字符串吗?
How can I find out if Boost any contains a literal string?
我如何找出Boost中是否包含文字字符串?
String literals are arrays of const char
. 字符串文字是
const char
数组。 boost::any
stores decayed types, so string literal will be a const char*
. boost::any
存储衰减类型,因此字符串文字将是const char*
。
Note that there is no guarantee for the const char*
to be a string literal. 注意,不能保证
const char*
是字符串文字。 It can be a pointer to any character, not just the first character of a string literal. 它可以是指向任何字符的指针,而不仅仅是字符串文字的第一个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.