"C + + Considerations" 5 top-level const, the auto and decltype Type specifier

Source: Internet
Author: User

top-level const

As we ' ve seen, a pointer is an object and can point to a different object. As a result, we can talk independently about whether a pointer be const and whether the objects to which it can point be 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, the 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 has both top-level and low-level const independently:

intI=0;int*Constp1= &i;//We can ' t change the value of P1;const is top-levelConst intCi= the;//We can ' t change ci;const is top-levelConst int*p2= &ci;//We can ' t change p2;cosnt is low-levelConst int*Constp3= P2;//Right-most const is Top-level,left-most are notConst int&r= CI;//const in reference types are 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 had to know the type of the. When we write a program, it can be surprisingly difficult–and sometimes even impossible–to determine the type of a expres Sion. 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-deduce the type from the I Nitializer. By implication, a variable-uses auto as its type specifier must has initializer:

theofitemfromtheoftheresultofanditem // item initialized to the result of vad1+vad2

Here the compiler would 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 is really using the object to which the reference refers. In particular, if 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:

0; &r= i;auto a= r;  isisaliasfortype int)

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

Constint ci= I, &cr= ci;auto b= ci;//B isan int (top-levelConst inchCi isDropped) Auto c= CR;//C isan int (CR isAn alias forCI whoseConst  istop-level) Auto d= &i;//D isAn int* (& ofan int object isint*) Auto e= &ci;//E isCosnt int* (& ofAConstObject isLow-levelConst)

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

const auto f= ci;  typeofistype const int

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

auto &g= ci;  isisto42;  // error: we can‘ttoliteral42;  toliteral

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

When we define several variables in the same statement, it's important to remember that a reference or Pinter are 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;  isis int&auto &m= ci, *p= &ci;  isistotypeistypeis 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 does not want to use tha T 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 and does not evaluate the expression:

decltype(f()) sum= x;  type f returns

Here, the compiler does isn't 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 of that would being returned if we were to call F.

The decltype handles top-level const and references differs subtly from the. When the expression to which we apply decltype are a variable, DECLTYPE returns the type of that variable, including Top-le Vel const and references:

00;  type const intdecltype(cj) y= x;  typeandisto x;decltype(cj) z;  isand must be initialized

Because CJ is a reference, Decltype (CJ) is a reference type. Like any other reference, Z must is initialized.

It is worth nothing that decltype are the only context in which a variable defined as a reference are not treated as a Synon Ym for the object to which it refers.

Decltype and References

When we apply decltype to an expression, which is not a variable, we get the type of that expression yields.

// decltype of an expression can be a reference typeint42*p= &i, &r= i;decltype(r+0) b;  //intintdecltype(*p) c;  //intand must be initialized

Here are 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 are an expression that yields a VA Lue that have a nonreference type.

On the other hand, the dereference operator are 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) are int&, not plain int.

Another important difference between decltype and auto are that the deduction do by Decltype depends on the form of its G Iven expression. What can are confusing is, 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 would evaluate the operand as an expression . A variable is an expression, the can be the left-hand side of A assignment. As a result, decltype on such an expression yields a reference:

ofavariablea // error: d is int& and must be initialized // ok: e is an (uninitialized) int

Remember that Decltype ((variable)) (note,double parentheses) was always a reference type, but Decltype (variable) was Referenc E type only if variable is a reference.

Thank you for your visit and hope to help you. We welcome your attention, collection and comment.

My more blog post: nomasp blog Guide

To make this article get treatise and ask questions, reprint please indicate the source:
Http://blog.csdn.net/nomasp

"C + + Considerations" 5 top-level const, the auto and decltype Type specifier

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.