Extraktion des Rückgabetypes



  • Gegeben ist eine mehrfach übeladene Funktion und ein Typ T. Es soll nun der Rückgabetyp der Funktion bestimmt werden, wenn ein T übergeben wird. In C++11 ist das trivial dank decltype. Gibt es jedoch in C++03 einen Trick das zu bewerkstelligen?

    Das Ganze kann man sich in etwa so vorstellen:

    int f(char);
    float f(bool);
    ...
    
    template<class T>
    struct s
    {
      typedef return_val<f(T())>::type type;
    };
    


  • boost::result_of


  • Mod

    Gibt es jedoch in C++03 einen Trick das zu bewerkstelligen?

    Naja, mittels Boost schon, wenns dich nicht stört:

    #include <boost/typeof/typeof.hpp>
    #include <boost/utility/declval.hpp>
    #include <boost/static_assert.hpp>
     #include <boost/type_traits/is_same.hpp>
    
    int f(char);
    float f(bool);
    
    template<class T>
    struct s
    {
    	typedef BOOST_TYPEOF(f(boost::declval<T>())) type;
    };
    
    int main()
    {
    	BOOST_STATIC_ASSERT(( boost::is_same<s<char>::type, int>::value ));
    }
    

    hetrazd schrieb:

    boost::result_of

    Das klappt doch gar nicht bei überladenen Funktionen in C++03? Ich meine, wie kommst du an den Typ der Funktion ran?


Log in to reply