[Effective modern C + +] Item 3. Understand Decltype-Learn about Decltype

Source: Internet
Author: User

about DecltypeBasic Knowledge

A variable or expression is provided, and Decltype returns its type, but the content returned is surprising.

Here are some simple inference types:

Const inti =0;//Decltype (i), const intBOOLFConstwidget& W);//Decltype (w), const widget&, Decltype (f), BOOL (const widget&)structPoint {intx, y;} //Decltype (point::x), intWidget W;//Decltype (w) Widgetif(f (w));//Decltype (f (w) ), BOOLTemplate<typename t>classVector { Public: T&operator[] (std::size_t index);}; Vector<int> v;//Decltype (v), vector<int>if(v[0] ==0);//Decltype (v[0]), int&

In c++11, the primary function of Decltype is to infer the return type inferred from the formal parameter type.

For std::vector<bool>,operator[] The return is not bool&, but a new object.

An inferred use of a return type:

Template<typename Container, TypeName index>Auto Authandaccess (Container& C, Index i)  Decltype (C[i]) {    authenticateuser ();     return c[i];}

C++11 allows inference for single-statement lambda return types, c+14 extends to all lambda and functions.

// c++14 version, but there will be a problem template<typename Container, typename index>Auto Authandaccess (Container & C, Index i) {    authenticateuser ();     return c[i];}

Although the above function uses auto, the rules that apply auto are problematic. As above C[i] returns INT&, but the reference type is removed according to the inference rule, causing no modification.

The following code can return the type correctly:

// c++14 version, the type Template<typename Container can be returned correctly , TypeName index> decltype(auto) authandaccess ( Container& C, Index i) {    authenticateuser ();     return c[i];}

The difference between auto and Decltype (auto):

Widget W; Const widget& CW =//  myWidget1 Widget//  MyWidget2, const widget& 

In order for the function to pass both Lvalue and Rvalue, the function introduces a generic reference, the correct form is as follows:

// final c++14 versionTemplate<typename Container, TypeName index>decltype (auto) authandaccess ( Container&& C, Index i) {    authenticateuser ();     return std::forward<container>(c) [i];} // final c++1 versionTemplate<typename Container, typename index>Auto Authandaccess (Container && C, Index i)decltype (std::forward<container>(c) [i]) {    authenticateuser () ;     return std::forward<container>(c) [i];}

When using Decltype, the parentheses outside the variable change the inferred type:

int 0  ///  int/ /- int&

Summary
    • Decltype always produces types of variables or expressions that have not been modified
    • For an lvalue expression of type T and not a variable name, Decltype always returns the t& type
    • C++14 supports Decltype (auto), which infers types from initialization like auto, but uses Decltype to infer types

[Effective modern C + +] Item 3. Understand Decltype-Learn about Decltype

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.