gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-27 23:39:04 27 4
gpt4 key购买 nike

我有这个代码

#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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com