【发布时间】:2018-07-31 17:52:51
【问题描述】:
C++11 为函数中的返回类型引入了箭头符号(不知道名称):
template <typename T>
auto fun(T&& a) -> decltype(bar(a)){ ... }
但根据 scott meyer 的说法,使用 auto 作为返回类型本身会删除所有 const 和引用限定符(因为它遵循与模板推导相同的模式),因此惯用的方法是使用 decltype(auto) 来保留所有类型顶部的限定符。
但是,在这种情况下,auto 是否被推断为decltype(bar(a))?那么decltype(auto) 会是decltype(decltype(bar(a))) 吗?那会不会是多余的?
【问题讨论】:
-
不知道名字这叫做尾随返回类型。
-
如果你明确指定它,它不会推断任何东西。
-
这个
auto与auto somthing = …不同,所以它不会丢弃任何东西。再读一遍 Scott Meyer 的书。 -
decltype(auto)是一个有其含义的特殊事物,它不是decltype应用于auto推演的结果。