【发布时间】:2019-12-15 20:27:33
【问题描述】:
在 c++ 中,您可以比较两个 type_info 对象。
有std::any 类。它有一个成员.type(),它还会返回一个type_info 对象,告诉你它包含的类型。我可以使用typeid(THE_TYPE) 并比较两者。
以下代码有效:
std::any x = 6;
if (x.type() == typeid(int)) {
cout << "x is int";
}
但以下方法不起作用:
std::any x = "string literal";
if (x.type() == typeid(std::string)) {
cout << "x is string";
}
我做错了什么?如何检查变量是否为字符串?
【问题讨论】: