C++ 使用boost::any时出现错误C2451 else if(字符串s=boost::any_cast(a))

C++ 使用boost::any时出现错误C2451 else if(字符串s=boost::any_cast(a)),c++,boost,boost-any,C++,Boost,Boost Any,这条线给你带来了麻烦。字符串s不是指针,它是堆栈变量。不能检查空值 可以对下面的整数进行检查的原因是整数隐式映射到bool。 0->FALSE 1->TRUE这里不应该对引用使用any\u cast,因为如果类型不正确,它会抛出bad\u any\u cast异常。在后两种情况下使用指针,就像在前三种情况下一样: else if(string s = boost::any_cast<string>(a)) else if(string*s=boost::any_cast(&a))

这条线给你带来了麻烦。字符串s不是指针,它是堆栈变量。不能检查空值

可以对下面的整数进行检查的原因是整数隐式映射到bool。
0->FALSE

1->TRUE

这里不应该对引用使用
any\u cast
,因为如果类型不正确,它会抛出
bad\u any\u cast
异常。在后两种情况下使用指针,就像在前三种情况下一样:

else if(string s = boost::any_cast<string>(a))
else if(string*s=boost::any_cast(&a))
{
库特
 error C2451: conditional expression of type 'std::string' is illegal
          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
else if(string s = boost::any_cast<string>(a))
else if(string* s = boost::any_cast<string*>(&a))
{
    cout << *s << endl;
}
else if(int* i = boost::any_cast<int*>(&a))
{
    cout << *i << endl;
}