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

來源:互聯網
上載者:User

【C++注意事項】5 Top-level const , The auto and decltype Type Specifier,top-leveldecltype
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-levelconst int ci= 42;  // we can't change ci;const is top-levelconst int *p2= &ci;  // we can't change p2;cosnt is low-levelconst int *const p3= p2;  // right-most const is top-level,left-most is notconst 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 val2auto 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 ciauto &h= 42;  // error: we can't bind a plain reference to a literalconst 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 intauto &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 intdecltype(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 typeint i= 42, *p= &i, &r= i;decltype(r+0) b;  // ok: addition yields an int; b is an (uninitialized) intdecltype(*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 referencedecltype((i)) d;  // error: d is int& and must be initializeddecltype(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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.