Search code examples
c++c++11trailing-return-type

Can the return type of the function be obtained from within the function?


Can the return type of a function be obtained in a simple way within the function?

For example, given:

template <typename P>
static inline auto foo(P p) -> typename std::remove_reference<decltype(*p)>::type {
    typename std::remove_reference<decltype(*p)>::type f{};  // <-- here

    ...
}

In C++11 can I refer to the big nasty return type of foo, within foo itself, without repeating it, at the line marked // <-- here?


Solution

  • Call the function with a decltype.

    decltype(foo(p)) f{};