decltype c++11

    Sometimes we want to define a variable with a type that the compiler deduces from a expression but do not want to use that expression to initialize the variable. For such cases, the new standard introduced a second type specifier, decltype which return the type of its operand. The compiler analyzes the expression to determine its type but does not evaluate the expression:
    decltype(f()) sum = x;
    Here, the compiler does not call f, but it use the type that such a call would return as the type for sum. That is, the compiler gives sum the same type as the type that would be returned if we were to call f.
    The way decltype handles top-level const and references differs subtly from the way auto does. When the expression to which we apply decltype is a variable, decltype returns the type of that variable, including top-level const and references:
    const int ci = 0, &cj = ci;
    decltype(ci) x = 0;    // x has type const int
    decltype(cj) y = 0;    // y has const int & and is bound to x
    decltype(cj) z;        // error: z is a reference and must be initialized
Because cj is a reference, decltype(cj) is a reference type. Like any other reference, z must be initialized.
    It is worth noting that decltype is the only context in which a variable defined as a reference is not treated as a synonym for the object to which it refers.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值