Geo*_*ica 7 c++ trailing-return-type return-type-deduction c++14
是否有任何理由再使用以下语法:
template<typename T>
auto access(T& t, int i)
-> decltype(t[i])
{
return t[i];
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以使用:
template<typename T>
decltype(auto) access(T& t, int i)
{
return t[i];
}
Run Code Online (Sandbox Code Playgroud)
现在,尾随返回类型语法似乎有点多余?
Jus*_*tin 17
退回的退货类型不是SFINAE友好的.如果t[i]无效,这个重载将简单地从重载集中退出:
template<typename T>
auto access(T& t, int i)
-> decltype(t[i])
{
return t[i];
}
Run Code Online (Sandbox Code Playgroud)
虽然这种重载不会导致硬错误:
template<typename T>
decltype(auto) access(T& t, int i)
{
return t[i];
}
Run Code Online (Sandbox Code Playgroud)
此外,您可能会遇到冲突的推断返回类型的问题.考虑我是否想要返回std::optional<T>.以下代码无法编译,因为std::nullopt_t它的类型不同std::optional<T>:
#include <optional> // C++17 standard library feature
template <typename T>
auto foo(T const& val)
{
if (val.is_invalid()) return std::nullopt;
return val.some_function_returning_an_optional();
}
Run Code Online (Sandbox Code Playgroud)
使用尾随返回类型可以准确指定要返回的表达式类型:
template <typename T>
auto foo(T const& val)
-> decltype(val.some_function_returning_an_optional())
{
if (val.is_invalid()) return std::nullopt;
return val.some_function_returning_an_optional();
}
Run Code Online (Sandbox Code Playgroud)
你可以使用一个领先的返回类型,但它需要使用std::declval,这使得它更难理解:
template <typename T>
decltype(std::declval<T const&>().some_function_returning_an_optional())
foo(T const& val)
{
if (val.is_invalid()) return std::nullopt;
return val.some_function_returning_an_optional();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
447 次 |
| 最近记录: |