You can do this with std::declval , as shown below:
#include <iostream> #include <utility> struct test { int val = 10; }; class Foo { public: test get_value() { return test(); } }; class Bar { public: using type = decltype(std::declval<Foo>().get_value()); }; int main() { Bar::type v; std::cout << v.val << std::endl; }
Live demo
std::declval<T> converts any type T to a reference type, allowing you to use member functions in decltype expressions without having to go through constructors.
std::declval usually used in templates where valid template parameters may not have a common constructor but have the same member function whose return type is needed.
101010
source share