函数的return类型可以从函数内部获取吗?
Can the return type of the function be obtained from within the function?
函数的return类型是否可以在函数内部简单获取?
例如,给定:
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
的 big nasty return 类型,而无需重复,在标记为 [=14= 的行]?
用 decltype
调用函数。
decltype(foo(p)) f{};
函数的return类型是否可以在函数内部简单获取?
例如,给定:
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
的 big nasty return 类型,而无需重复,在标记为 [=14= 的行]?
用 decltype
调用函数。
decltype(foo(p)) f{};