在c ++ 14 decltype(auto)中引入了成语.
通常,它的用途是允许auto声明使用decltype给定表达式的规则.
搜索成语的"好"用法示例我只能想到以下内容(由Scott Meyers提供),即函数的返回类型推导:
template<typename ContainerType, typename IndexType> // C++14
decltype(auto) grab(ContainerType&& container, IndexType&& index)
{
authenticateUser();
return std::forward<ContainerType>(container)[std::forward<IndexType>(index)];
}
Run Code Online (Sandbox Code Playgroud)
这个新语言功能有用吗?
如果在两种情况下都没有使用括号,那么函数(模板)的返回类型decltype(auto)和decltype(returning expression)返回类型之间有什么区别expr?
auto f() -> decltype(auto) { return expr; } // 1
auto f() -> decltype(expr) { return expr; } // 2
Run Code Online (Sandbox Code Playgroud)
上面f可以在任何上下文中定义/声明,可以是(成员)函数或(成员)函数模板,甚至是(泛型)lambda.expr可以依赖于任何模板参数.
在第二个版本中,两者expr都是完全相同的表达式,没有额外的括号.
在C++ 14及更高版本中使用第一种或第二种形式可以预期哪些差异?
如果括号到处使用怎么办?