相关疑难解决方法(0)

decltype(auto)有哪些用途?

在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)

这个新语言功能有用吗?

c++ decltype auto return-type-deduction c++14

133
推荐指数
2
解决办法
5万
查看次数

decltype(auto)和decltype(返回expr)作为返回类型有什么区别?

如果在两种情况下都没有使用括号,那么函数(模板)的返回类型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及更高版本中使用第一种或第二种形式可以预期哪些差异?

如果括号到处使用怎么办?

c++ decltype auto c++14

13
推荐指数
2
解决办法
1183
查看次数

标签 统计

auto ×2

c++ ×2

c++14 ×2

decltype ×2

return-type-deduction ×1