Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 425
JohnB Avatar asked Sep 01 '26 00:09

JohnB


2 Answers

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

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

Include the <utility> header.

like image 192
Cheers and hth. - Alf Avatar answered Sep 03 '26 13:09

Cheers and hth. - Alf


You can do this:

typedef decltype(*(T1*)0 + *(T2*)0) sum_type; 
like image 40
Vaughn Cato Avatar answered Sep 03 '26 14:09

Vaughn Cato



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!