Search code examples
c++templatesdecltype

C++ Template; Auto deduction return type of function passed as template argument;


I need to deduct a type of a return value of a getter function. That getter is passed as function pointer in template list.

So getter looks like:

using TGetter   = std::function<std::string(Me*)>;

std::string getter(Me* me)
{
    return std::string("string");
}

Template class

template<typename TGetter>
class Handler
{
public:
    Handler(TGetter getter)
        :m_getter(getter)
    {};

    using TValue =  std::string;  //decltype(TGetter()); <- something like that I want to get

private:
    TGetter                     m_getter;
    ...

    bool handler();

Instantiation is like that:

Handler<TGetter> h(getter);

What I want is to declare TValue depending on getter return type. So std::string as for getter in example. I'm gonna have different types of getters and wand to declare respective type simply like TValue value; inside a class.

using TValue = decltype(TGetter()); is resolved into a function pointer.

Could you help me to get it right, thanks.


Solution

  • What you are looking for is result_type, probably:

    //...
    public:
      using TValue =  typename TGetter::result_type;
    //...
    

    Demo.