Getting the type of the returned object of the Templatized Object method

Say what I have:

template <typename T>
struct Foo {
    T& func();
};

And I implement Foo: Foo<int> barNow I want to get the return type bar.func(). I tried to get result_ofto work with me, but to no avail.

I would really like to just do result_of_t<foo.func>and do with it, but I think it is much more complicated? How do I get this type of refund?

EDIT: I was hoping to do this without respect for how it was announced bar. That is, I just want to pass bar.funcin result_ofor similar and gt outside the return type.

+4
source share
1

std::result_of . :

 result_of<F(ArgTypes...)>

F - - invokable, - . -: &Foo<int>::func. --, , . decltype(&Foo<int>::func). - - .

, :

using T = std::result_of_t<decltype(&Foo<int>::func)(Foo<int>&)>;
static_assert(std::is_same<T, int&>::value, "!");

decltype:

using T = decltype(std::declval<Foo<int>&>().func());

.


bar, :

using T = decltype(bar.func());

:

using T = std::result_of_t<decltype(&decltype(bar)::func)(decltype(bar))>;
+6

Source: https://habr.com/ru/post/1609994/


All Articles