设为首页 加入收藏

TOP

2.5 Dealing with Types (3)
2013-10-07 16:16:56 来源: 作者: 【 】 浏览:82
Tags:2.5 Dealing with Types

2.5.3 The decltype Type Specifier

Sometimes we want to define a variable with a type that the compiler deduces from an 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 returns the type of its operand. The compiler analyzes the expression to determine its type but does not eva luate the expression:

  1. decltype(f()) sum = x; // sum has whatever type f returns  

Here, the compiler does not call f, but it uses 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.

Theway 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:

  1. const int ci = 0, &cj = ci;   
  2. decltype(ci) x = 0; // x has type const int   
  3. decltype(cj) y = x; // y has type const int& and is bound to x   
  4. 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.

decltype and References

When we apply decltype to an expression that is not a variable, we get the type that that expression yields. As we’ll see in § 4.1.1 (p. 135), some expressions will cause decltype to yield a reference type. Generally speaking, decltype returns a reference type for expressions that yield objects that can stand on the left-hand side of the assignment:

  1. // decltype of an expression can be a reference type   
  2. int i = 42, *p = &i, &r = i;   
  3. decltype(r + 0) b; // ok: addition yields an int; b is an (uninitialized) int   
  4. decltype(*p) c; // error: c is int& and must be initialized  

Here r is a reference, so decltype(r) is a reference type. If we want the type to which r refers,we can use r in an expression, such asr + 0, which is an expression that yields a value that has a nonreference type.

On the other hand, the dereference operator is an example of an expression for which decltype returns a reference. As we’ve seen, when we dereference a pointer, we get the object to which the pointer points. Moreover, we can assign to that object. Thus, the type deduced by decltype(*p) is int&, not plain int.

Another important difference between decltype and auto is that the deduction done by decltype depends on the form of its given expression. What can be confusing is that enclosing the name of a variable in parentheses affects the type returned by decltype. When we apply decltype to a variable without any parentheses, we get the type of that variable. If we wrap the variable’s name in one or more sets of parentheses, the compiler will eva luate the operand as an expression. A variable is an expression that can be the left-hand side of an assignment. As a result, decltype on such an expression yields a reference:

  1. // decltype of a parenthesized variable is always a reference   
  2. decltype((i)) d; // error: d is int& and must be initialized   
  3. decltype(i) e; // ok: e is an (uninitialized) int  

Remember that decltype((variable)) (note, double parentheses) is always a reference type, but decltype(variable) is a reference type only if variable is a reference.


EXERCISES SECTION 2.5.3

Exercise 2.36: In the following code, determine the type of each variable and the value each variable has when the code finishes:

  1. int a = 3, b = 4;   
  2. decltype(a) c = a;   
  3. decltype((b)) d = a;   
  4. ++c;   
  5. ++d;  

Exercise 2.37: Assignment is an example of an expression that yields a reference type. The type is a reference to the type of the left-hand operand. That is, if i is an int, then the type of the expression i = x is int&. Using that knowledge, determine the type and value of each variable in this code:

  1. int a = 3, b = 4;   
  2. decltype(a) c = a;   
  3. decltype(a = b) d = a;  

Exercise 2.38: Describe the differences in type deduction between decltype and auto. Give an example of an expression where auto and decltype will deduce the same type and an example where they will deduce differing types.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.5 Dealing with Types (1) 下一篇2.6 Defining Our Own Data Struc..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·用 Python 进行数据 (2025-12-25 15:49:09)
·如何学习Python数据 (2025-12-25 15:49:07)
·利用Python进行数据 (2025-12-25 15:49:04)
·Java 学习线路图是怎 (2025-12-25 15:19:15)
·关于 Java 学习,有 (2025-12-25 15:19:12)