August 2017
Intermediate to advanced
438 pages
14h 19m
English
Boost offers a solution, the Boost.Any library, that has an even better syntax:
#include <boost/any.hpp> #include <iostream> #include <vector> #include <string> int main() { std::vector<boost::any> some_values; some_values.push_back(10); some_values.push_back("Hello there!"); some_values.push_back(std::string("Wow!")); std::string& s = boost::any_cast<std::string&>(some_values.back()); s += " That is great!"; std::cout << s; }
Great, isn't it? By the way, it has an empty state, which could be checked using the empty() member function (just like in standard library containers).
You can get the value from boost::any using two approaches:
void example() { boost::any variable(std::string("Hello world!")); // Following ...Read now
Unlock full access