23rd Chapter C++11 Characteristics of the Decltype

Source: Internet
Author: User

Decltype

Similar to the sizeof operator, Decltype does not need to evaluate its operands. Roughly speaking, Decltype (e) is deduced as follows before returning a type:

1. If the expression e points to a local variable, namespace scope variable, static member variable, or function parameter, then the return type is the "declaration type" of the variable (or parameter);

2. If E is an lvalue (Lvalue, or "addressable value"), then Decltype (e) will return T&, where T is the type of E;

3. If E is an X value (Xvalue), the return value is t&&;

4. If e is a pure right value (prvalue), the return value is T.

These semantics are designed to meet the requirements of the General library writer, but because the return type of decltype always matches the definition type of the object (or function), this is more intuitive for novice programmers. More formally, rule 1 applies to non-parenthesized identifier expressions (id-expression) and class-member access expressions. Examples are as follows:

Const int&& foo (); const int bar (); int i;struct A {double x;};

CONST * A = new A ();

Decltype (foo ()) x1; Type is const int&&

Decltype (Bar ()) X2; Type is int

Decltype (i) X3; Type is int

Decltype (a->x) x4; Type is double

Decltype ((a->x)) X5; Type is const double&

As seen above, the last two calls to Decltype, the returned results are different. This is because the parenthesized expression (a->x) is neither an "identifier expression" nor a class-access expression, so it does not point to a named object but an lvalue, so the deduction type is "a reference to the expression type", that is, the const double&.

Decltype is actually a bit like auto's inverse function, Auto lets you declare a variable, and decltype can get the type from a variable or an expression, with the following example:

int x = 3;decltype (x) y = x;

Some people will ask, where is the utility of Decltype, and we continue with the above example, if we want to use the product as the return value in the example of the processed product above? We can write this:

Template <typename Creator>auto processproduct (const creator& Creator), Decltype (Creator.makeobject ()) {    Auto val = Creator.makeobject ();    Do somthing with Val}

return value decltype (expression)

[The type of the return value is the type of the expression parameter]

This can also be used to determine the type of expression, as Bjarne implies, if we need to initialize some type of variable, auto is the simplest choice, but if the type we want is not a variable, such as a return value then we can try Decltype.

Now let's look at some examples of what we've done before,

[CPP]View Plaincopy
    1. Template <class U, class v>
    2. void Somefunction (U u, v v)
    3. {
    4. result = U*v;  //now What type would is the result???
    5. Decltype (u*v) result = U*v; //hmm .... We got what we want
    6. }

In the following paragraph I will acquaint you with this idea. Use Auto and Decltype to declare the return value of a template function whose type depends on the template parameter.

1. If the expression is a function, the type given by Decltype is the type of the function return value.

[CPP]View Plaincopy
    1. int add (int i, int j) { return i+j;}
    2. Decltype (Add (5,6)) var = 5; //here the type of VAR is return of Add (), which is int

2. If the expression is an lvalue type, the type given by Decltype is the expression lvalue reference type.

[CPP]View Plaincopy
  1. struct M { double x;};
  2. Double pi = 3.14;
  3. Const m* m = new M ();
  4. Decltype ((m->x)) piref = pi;
  5. //Note:due to the inner bracets the inner statement is evaluated as expression,
  6. //rather than member ' X ' and as type of x is a double and as this is Lvale
  7. //The Return of Declspec is double& and as ' m ' is a const pointer
  8. //The return is actually const double&.
  9. //So the type of piref is const double&

3. Very important to mark, Decltype does not execute an expression and auto will, he merely infers the type of the expression.

[CPP]View Plaincopy
    1. int foo () {}
    2. Decltype (foo ()) x; //x is a int and note that
    3. //foo () isn't actually called at runtime


Trace return type:

This is a completely new feature for C + + developers, until the return type of the function is now placed in front of the function name. To C++11, we can also put the type of the function return value after the function declaration, of course, only need to replace the return type with auto. Now we want to know what to do, let's look for answers:

[CPP]View Plaincopy
    1. template<class U, class v>
    2. ??? Multiply (U u, v v) // How to specifiy the type of the return value
    3. {
    4. return u*v;
    5. }

We obviously can't do it like this:

[CPP]View Plaincopy
    1. template<class U, class v>
    2. Decltype (U*V) Multiply (U u, v v) //Because u & V is not defined before Multiply.
    3. //WHAT to Do...what!!!
    4. {
    5. return u*v;
    6. }


in this case we can also use auto and then when we use Decltype (U*V) as the return value this type is known.

Isn't that cool?

[CPP]View Plaincopy
      1. template<class U, class v>
      2. Auto Multiply (U u, v v), Decltype (U*V) //Note, after the function Bracet.
      3. {
      4. return u*v;
      5. }

23rd Chapter C++11 Characteristics of the Decltype

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.