got a bad_any_cast exception

#include <iostream> #include <boost/any.hpp> template<typename A, typename B> boost::any min(const A& a,const B& b) { return a < b ? a : b; } void main() { //got a bad_any_cast exception ! std::cout << boost::any_cast<int>( min( 1, 1.1)) << std::endl; } how could this happen?

only boost::any_cast<double> can work.. "ClassyK_Danad" <classyk@163.com> ???? news:eiu90m$48t$1@sea.gmane.org...
#include <iostream> #include <boost/any.hpp>
template<typename A, typename B> boost::any min(const A& a,const B& b) { return a < b ? a : b; }
void main() { //got a bad_any_cast exception ! std::cout << boost::any_cast<int>( min( 1, 1.1)) << std::endl; }
how could this happen?

ClassyK_Danad wrote:
#include <iostream> #include <boost/any.hpp>
template<typename A, typename B> boost::any min(const A& a,const B& b) { return a < b ? a : b; }
void main() { //got a bad_any_cast exception ! std::cout << boost::any_cast<int>( min( 1, 1.1)) << std::endl; }
Because in your example, min is returning a double, and you're trying to extract an int. -- Eric Niebler Boost Consulting www.boost-consulting.com
participants (3)
-
ClassyK_Danad
-
Eric Niebler
-
Sebastian Redl