是否可以从函数内部获取函数的返回类型?

时间:2018-09-05 17:33:19

标签: c++ c++11 trailing-return-type

可以在函数内以简单的方式获得函数的返回类型吗?

例如,给定:

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

    ...
}

在C ++ 11中,我是否可以在标记为foo的行中引用foo内的// <-- here本身,而不必重复它呢?

1 个答案:

答案 0 :(得分:50)

使用decltype调用函数。

decltype(foo(p)) f{};
相关问题