Summary of objective Modern C ++ Item 3, inclutivemodernc

Source: Internet
Author: User

Summary of objective Modern C ++ Item 3, inclutivemodernc
Decltype Summary

The first noteworthy problem is that operator [] on a container of objects of type T typically returns a T &. [] The return type is usually returned when the T & operator is used to store containers of T-type objects. Specific problems need to be analyzed. The decltype is a [] operator, and the return type depends on the container that stores the T-type objects. Vector <bool> is special.

template<typename Container, typename Index> // works, butauto authAndAccess(Container& c, Index i) // requires-> decltype(c[i]) // refinement{authenticateUser();return c[i];}

This section of the C ++ 11 code is valid, but there is a problem. We use location return type and c and I. If other functions cannot obtain c and I, we cannot use decltype. This method can be improved.

template<typename Container, typename Index> // C++14;auto authAndAccess(Container& c, Index i) // not quite{ // correct authenticateUser(); return c[i]; // return type deduced from c[i]}

This section of C ++ 14 code is incorrect. From terms 1 and 2, we can see that auto recognizes the left value reference as the part that removes the reference. Here we actually need to return the left value reference.

template<typename Container, typename Index> // C++14; works,decltype(auto) // but stillauthAndAccess(Container& c, Index i) // requires{ // refinementauthenticateUser();return c[i];}

Decltype (auto) is finally done. We don't need to set the return type at the end, and the type is recognized as a reference of the Left value. However, this method still needs to be improved.

Because we still need to face a problem, this template cannot use the right value. Therefore, a temporary container object cannot be passed in. We need to support operations similar to the following

std::deque<std::string> makeStringDeque(); // factory function// make copy of 5th element of deque returned// from makeStringDequeauto s = authAndAccess(makeStringDeque(), 5);

So we need to introduce new words, which can be referenced globally and forwarded perfectly. At this year's C ++ Standards Committee meeting, we decided to officially rename the universal references as forwarding references (forwarding references ), forwarding references are generally used and perfect forwarding. You need to have a deep understanding of the two functions std: forward and std: move, and the difference between the right value reference and the left value reference, in addition, I plan to write an article specifically for the right value reference.

template<typename Container, typename Index> // finaldecltype(auto) // C++14authAndAccess(Container&& c, Index i) // version{ authenticateUser(); return std::forward<Container>(c)[i];}

Everything is done.

Pay attention to some minor issues, such

int x;decltype((x)) //type is int&

More details:

1) If the argument is either the unparenthesised name of an object/function, sion ( object.memberOr pointer->Or is a member access expres member), Then the decltype specifies the declared type of the entity specified by this expression.2) If the argument is any other expression of type T, Thena) if the value category of expression is Xvalue, Then the decltype specifies T&&B) if the value category of expression is Lvalue, Then the decltype specifies T&C) otherwise, decltype specifies T

Except for the special brackets, the type of decltype (variable, only the expression of the name) is always a reference. If it is a simple expression (only objec. member is simple), decltype is the type of the returned object.

For any other complex expressions, follow 2 ),

Here is a link in English. For reference, what is left, right, and x?



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: 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.