
I found a way to avoid having to explicitly provide the type of the variable value that I was trying to extract from the variable_map. In environments that support the C++0x standard, you can use the std::map.at() function to get generic access to the variable's value(Trying to use operator[]() usually results in a compiler error.): // Given the function Foo as: void Foo(unsigned short val); // The following are equivalent Foo(vm["my_ushort"].as<unsigned short>()); Foo(vm.at("my_ushort")); Granted, this won't work in cases where the destination type is ambiguous or undiscoverable (ie: if Foo was overloaded to accept floats as well), but for many cases the convenience is nice. In those cases where your destination type does not match the variable type, you will get a bad_any_cast exception, just as you would with "as()". Does anyone think would be a worthwhile addition to program_options? Thanks, Josh # svn diff against the trunk Index: boost/program_options/variables_map.hpp =================================================================== --- variables_map.hpp (revision 72996) +++ variables_map.hpp (working copy) @@ -74,6 +74,23 @@ return boost::any_cast<T&>(v); } + /** Same as "as()" but allows you to use a simpler syntax: + Ex: // Given the function Foo as: + void Foo(unsigned short val); + // The following are equivalent + Foo(vm["my_ushort"].as<unsigned short>()); + Foo(vm.at("my_ushort")); */ + template<class T> + operator const T& () const { + return boost::any_cast<const T&>(v); + } + + /** @overload */ + template<class T> + operator T& () { + return boost::any_cast<T&>(v); + } + /// Returns true if no value is stored. bool empty() const; /** Returns true if the value was not explicitly