Martin C. Martin
Martin C. Martin

Reputation: 3672

decltype for member functions

How can I get the return type of a member function in the following example?

template <typename Getter>
class MyClass {
   typedef decltype(mygetter.get()) gotten_t;
 ...
};

The problem, of course, is that I don't have a "mygetter" object while defining MyClass.

What I'm trying to do is: I'm creating a cache that can use, as it's key, whatever is returned by the getter.

Upvotes: 6

Views: 1062

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120711

I'm not quite sure what you want, but it seems mygetter is supposed to be simply any object of type Getter. Use std::declval to obtain such an object without anything else (you can only use it for type deduction)

typedef decltype(std::declval<Getter>().get()) gotten_t;

Upvotes: 11

Related Questions