JohnB
JohnB

Reputation: 13713

decltype on type expressions

Is there any way to avoid the dummy functions in the following example?

template<class T1, class T2>
struct A {

    static T1 T1_ ();
    static T2 T2_ ();

    typedef decltype (T1_ () + T2_ ()) sum_type;
};

I would like to write

typedef decltype (T1+T2) sum_type;

but that's not possible since T1 and T2 are types, not variables. Is my above solution really the easiest one possible?

Upvotes: 3

Views: 91

Answers (2)

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145279

The Holy Standard provides std::declval for exactly this purpose:

typedef decltype (declval<T1>()+declval<T2>()) sum_type;

Include the <utility> header.

Upvotes: 5

Vaughn Cato
Vaughn Cato

Reputation: 64308

You can do this:

typedef decltype(*(T1*)0 + *(T2*)0) sum_type; 

Upvotes: 4

Related Questions