How do we know which container data is present in boost variant? My variant is having any datatype or container. how to access the type of the data from the container?
boost::variant<string, int> myvar = 245;
if(myvar.type() == typeid(int)){
std::cout<<"integer data type"<< '\n';//I am able to get the type of integer/string
}
boost::variant<vector<int>, list<string>> v {1,2,3,4};//???
boost::variant<vector<int>, list<string>> v {1,2,3,4};//???fails because the compiler can not determine the type from the given initializer list.
You simply can provide the type and all works as expected:
BTW: Why use
boost::variant? You also havestd::variantwhich fits maybe better?Answering the questions in the comments: see full example below!
Live