Aftershock
Aftershock

Reputation: 5351

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?

Upvotes: 0

Views: 206

Answers (1)

eerorika
eerorika

Reputation: 238351

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

String literals are arrays of const char. boost::any stores decayed types, so string literal will be a const char*.

Note that there is no guarantee for the const char* to be a string literal. It can be a pointer to any character, not just the first character of a string literal.

Upvotes: 3

Related Questions