c++ - 如何确定 boost::any 是否包含文字字符串?

标签 c++ boost

我有这个代码

#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*";
    }
}

我应该使用什么 if 语句来检测 “hello” 或任何大小的文字字符串?

最佳答案

How can I find out if Boost any contains a literal string?

字符串文字是const char 的数组。 boost::any 存储衰减类型,因此字符串文字将是 const char*

请注意,不能保证 const char* 是字符串文字。它可以是指向任何字符的指针,而不仅仅是字符串文字的第一个字符。

关于c++ - 如何确定 boost::any 是否包含文字字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56974599/

相关文章:

c++ - "LRESULT CALLBACK WinProc"和 "LRESULT WINAPI WinProc"之间的区别

c++ - 从 operator<< 调用纯虚函数

c++ - 当我收到 `slicing` 的案例时?

c++ - 在 Windows 中使用 cygwin 编译 ncmpcpp

c++ - Boost.Asio iostream 冲洗不工作?

c++ - 无向 DFS : how can I provide color maps as exterior properties?

c++ - 长期绘制密集粒子系统的良好且安全的语言是什么?

c++ - 使用 typedef 作为变量名不会产生任何错误

c++ - 如何检测表达式类型?

c++ - 使用正则表达式在标准映射中查找