boost-any

How to print boost::any to a stream?

让人想犯罪 __ 提交于 2019-11-27 14:03:39
I have a Map std::map<std::string, boost::any> , which comes from the boost::program_options package. Now I would like to print the content of that map: for(po::variables_map::const_iterator it = vm.begin(); it != vm.end(); ++it) { std::cerr << it->first << ": " << it->second << std::endl; } Unfortunately, that is not possible because boost::any doesn't have an operator<< defined. What is the easiest way to print that map? I could define my own output operator for any that automatically tries to cast each any to an int, then double, then string, etc., each time ignoring errors and trying to

How do boost::variant and boost::any work?

会有一股神秘感。 提交于 2019-11-26 18:51:26
问题 How do variant and any from the boost library work internally? In a project I am working on, I currently use a tagged union. I want to use something else, because unions in C++ don't let you use objects with constructors, destructors or overloaded assignment operators. I queried the size of any and variant, and did some experiments with them. In my platform, variant takes the size of its longest possible type plus 8 bytes: I think it my just be 8 bytes o type information and the rest being

How to print boost::any to a stream?

 ̄綄美尐妖づ 提交于 2019-11-26 16:35:01
问题 I have a Map std::map<std::string, boost::any> , which comes from the boost::program_options package. Now I would like to print the content of that map: for(po::variables_map::const_iterator it = vm.begin(); it != vm.end(); ++it) { std::cerr << it->first << ": " << it->second << std::endl; } Unfortunately, that is not possible because boost::any doesn't have an operator<< defined. What is the easiest way to print that map? I could define my own output operator for any that automatically tries