3

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?

1
  • 2
    typedef decltype(*(T1*)0 + *(T2*)0) sum_type; avoids the functions. Commented Nov 17, 2012 at 6:30

2 Answers 2

5

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

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

Include the <utility> header.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Exactly what I need. Never heard about it. Visual C++ 2010 does not seem to have it, but now declared it myself by template<class T> T declval ();.
@JohnB: The correct one is template<class T> typename std::add_rvalue_reference<T>::type declval();.
Hm, shouldn't be the result of a function an rvalue anyway? I fully trust in what you say, but I do not understand why simple T does not suffice.
4

You can do this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.