《 Effective Modern C++》真是本好书,,讲的深入浅出,非常清晰
Given a name or an expression, decltype tells you the name’s or the expression’s type
const int i = 0;            // decltype(i) is const int
bool f(const Widget& w);    // decltype(w) is const Widget&
                            // decltype(f) is bool(const Widget&)
template<typename T>
class vector {
public:
    T& operator[](std::size_t index);
};
vector<int> v;              // decltype(v) is vector<int>
                            // decltype(v[0]) is int&
如何查看 decltype(f)、decltype(v[0])的值?
template<typename T>
class TD;                   // Type Displayer
TD<decltype(v[0])> x;
编译如上代码,编译器会报错如下:
error: aggregate 'TD<int&> x' has incomplete type and cannot be defined
由此可知 decltype(v[0])的结果是int&
auto specifies that the type is to be deduced, and decltype says that decltype rules should be used during the deduction
template<typename Container, typename Index>
decltype(auto) authAndAccess(Container& c, Index i)
{
    authenticateUser();
    return c[i];
}
这里之所以使用 decltype(auto)而不是 auto,是因为绝大多数 containers-of-T 的[]运算符返回 T&,用 auto 的话会丢失&属性
if an lvalue expression other than a name has type T, decltype reports that type as T&
int x = 3;      // decltype(x)   is int
                // decltype((x)) is int&
注意:decltype(-x)等不会出现这种问题,因为-x等表达式都是右值,只有(x)既是表达式还是左值
这将导致下面两个函数的返回值类型不同
decltype(auto) f1()
{
    int x = 0;
    return x;
}
decltype(auto) f2()
{
    int x = 0;
    return (x);
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.