码迷,mamicode.com
首页 > 编程语言 > 详细

【C++注意事项】5 Top-level const , The auto and decltype Type Specifier

时间:2015-05-21 22:38:05      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:point   level   const   c++   pointer   

top-level const

As we’ve seen, a pointer is an object that can point to a different object. As a result, we can talk independently about whether a pointer is const and whether the objects to which it can point are const. we use the top-level const to indicate that the pointer itself is a const. When a pointer can point to a const object, we refer to that const as a low-level const.

More generally, top-level const indicates that an object itself is const. Top-level const can appear in any object type,i.e., one of the built-in arithmetic types, a class type, or a pointer type. Low-level const appears in the base type of compound types such as pointer or reference. Note that pointer types, unlike most other type, can have both top-level and low-level const independently:

int i= 0;
int *const p1= &i;  // we can‘t change the value of p1;const is top-level
const int ci= 42;  // we can‘t change ci;const is top-level
const int *p2= &ci;  // we can‘t change p2;cosnt is low-level
const int *const p3= p2;  // right-most const is top-level,left-most is not
const int &r= ci;  // const in reference types is always low-level

The auto Type Specifier

It is not uncommon to want to store the value of an expression in a variable. To declare the variable, we have to know the type of that expression. When we write a program, it can be surprisingly difficult–and sometimes even impossible–to determine the type of an expression. Under the new standard, we can let the compiler figure out the type for us by using the auto type specifier. Unlike type specifiers, such as double, that names a specifier type, auto tells the compiler to deduce the type from the initializer. By implication, a variable that uses auto as its type specifier must have initializer:

// the type of item is deduced from the type of the result of adding val1 and val2
auto item= val1+val2;  // item initialized to the result of vad1+vad2

Here the compiler will deduce the type of item from the type returned by applying + to val1 and val2.

First, as we’ve seen, when we use a reference, we are really using the object to which the reference refers. In particular, when we use a reference as in initializer, the initializer is the corresponding object. The compiler uses that object’s type for auto’s type deduction:

int i= 0; &r= i;
auto a= r;  // a is an int(r is an alias for i, which has type int)

Second, auto ordinarily ignores top-level consts. As usual in initializations, low-level consts, such as when an initializer is a pointer to const, are kept:

const int ci= i, &cr= ci;
auto b= ci;  // b is an int(top-level const in ci is dropped)
auto c= cr;  // c is an int(cr is an alias for ci whose const is top-level)
auto d= &i;  // d is an int*(& of an int object is int*)
auto e= &ci;  // e is cosnt int*(& of a const object is low-level const)

If we want the deduced type to have a top-level const, we must say so explicitly:

const auto f= ci;  // deduced type of ci is int; f has type const int

We can also specify that we want a reference to the auto-deduced type. Normal initialization rules still apply:

auto &g= ci;  // g is a const int& that is bound to ci
auto &h= 42;  // error: we can‘t bind a plain reference to a literal
const auto &j= 42;  // ok: we can bind a const reference to a literal

when we ask for a reference to an auto-deduced type, top-level consts in the initializer are not ignored. As usual, consts are not top-level when we bind a reference to an initializer.

When we define several variables in the same statement, it is important to remember that a reference or pinter is part of a particular declarator and not part of the base type for the declaration. As usual, the initializers must provide consitent auto-deduced types:

auto k= ci, &l= i;  // k is int; l is int&
auto &m= ci, *p= &ci;  // m is a const int&; p is a pointer to cosnt int
// error: type deduced from i is int; type deduced from &ci is const int
auto &n= i, *p2= &ci;

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 evaluate the expression:

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.

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:

cosnt int ci= 0, &cj= ci;
decltype(ci) x= 0;  // x has type const int
decltype(cj) y= x;  // y has type cosnt int& and is bound to x;
decltype(cj) z;  // error: z is 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 nothing 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 expression yields.

// decltype of an expression can be a reference type
int i= 42, *p= &i, &r= i;
decltype(r+0) b;  // ok: addition yields an int; b is an (uninitialized) int
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 as r+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 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 warp the variable’s name in one or more sets of parentheses, the compiler will evaluate 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:

// decltype of a parenthesized variable is always a reference
decltype((i)) d;  // error: d is int& and must be initialized
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 reference type only if variable is a reference.



感谢您的访问,希望对您有所帮助。 欢迎大家关注、收藏以及评论。

我的更多博客文章:NoMasp博客导读


为使本文得到斧正和提问,转载请注明出处:
http://blog.csdn.net/nomasp


【C++注意事项】5 Top-level const , The auto and decltype Type Specifier

标签:point   level   const   c++   pointer   

原文地址:http://blog.csdn.net/nomasp/article/details/45896981

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!