Decltype Summary
The first noteworthy question is, operator[] on a container of objects of type T typically returns a t&. The [] operator typically returns T& when storing a container of type T objects, and the specific problem requires specific analysis, decltype a [] operator, and the return type relies on the container that stores it. Vector<bool> is more special.
// works, but // requires // Refinement {authenticateuser (); return c[i];}
This c++11 code works, but there are problems, we use the positional return type, and with C and I, the decltype cannot be used for other functions if C and I are not available. This method can be perfected.
// c++14; // Not quite // correct return// return type deduced from c[i]}
This section of the C++14 code is not correct, from the clause one, two know, auto will recognize the Lvalue reference as the part to remove the reference, here we actually need to return an lvalue reference.
// c++14; works, // But still // requires // Refinement Authenticateuser (); return c[i];}
Decltype (Auto) finally done, we don't need a trailing return type, and the type is recognized as an lvalue reference. But this approach still needs to be perfected.
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::d eque<std::string// factory function// make copy of 5th element of Deque returned// from makestringdeque5);
Then need to introduce new words, global reference and perfect forwarding, this year, the C + + standards Committee to open a general meeting, decided to formally renamed the Universal References forwarding references (forward reference), the forward reference is generally used with the perfect forwarding, You need to know more about the two functions of Std::forward and Std::move, as well as the difference between rvalue reference and Lvalue reference, this follow-up clause has to say, and I also intend to write an article specifically for rvalue references.
// Final // c++14 // version return std::forward<container>(c) [i];}
So it's all done.
Finally, you need to pay attention to some small problems, such as
int//type is int&
For more details:
1) If The argument is either the unparenthesised name of an object/function,sion (
object.member or
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 and thena) If the value category of expression is
xvalueand then the decltype specifies
T&& b) If the value category of expression is
lvalueand then the decltype specifies
T& c) Otherwise, decltype specifies
T
Except for the very special parentheses, the type of decltype (a variable, a name-only expression) is always a reference. If it is a simple expression (only the name of the Objec.member is simple), then Decltype is the type that returns it.
For any other more complex expression, follow 2),
Here is an English connection for reference to what is Lvalue, rvalue, x value
Http://en.cppreference.com/w/cpp/language/value_category
Summary of "effective modern C + +" Item 3