Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

c++ - How does auto deduce type?

I have some cases of use of auto:

auto s = expr;          //s is always lvalue
auto & s = expr;        //s is always lvalue reference? What if expr is rvalue?
auto && s = expr;       //s is perfectly forwarded

Are they true? If not, why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

dyp is correct and I would like to elaborate.

First of all, the conclusion is the from dyp:

The type deduced for auto in the declaration of a variable is defined via the rules of template argument deduction, see [dcl.spec.auto]/6; with one exception: if the initializer is a braced-init-list, the deduced type is a std::initializer_list.

I'll explain.

First,

auto s = expr;

This is same as deducing the T from expr,

template<class T>
void f(T s);

f(expr);

The rule for template argument deduction is quite complicated, since you are only concerning with the lvalue and rvalue stuff, let's focus on this.

Template argument deduction is by comparing the template parameter type (call it P, in this case P is T), and the corresponding argument (call it A, in this case, the type of expr).

From 14.8.2.1,

If P is not a reference type:

— If A is an array type, the pointer type produced by the array-to-pointer standard conversion (4.2) is used in place of A for type deduction; otherwise,

— If A is a function type, the pointer type produced by the function-to-pointer standard conversion (4.3) is used in place of A for type deduction; otherwise,

— If A is a cv-qualified type, the top level cv-qualifiers of A’s type are ignored for type deduction.

So, if expr is array or function, it will be treated as pointers, if expr has cv-qualification (const etc), they will be ignored.

If P is a cv-qualified type, the top level cv-qualifiers of P’s type are ignored for type deduction.

This actually says:

const auto s = expr;

s is a const variable, but for type deduction for auto purposes, the const will be removed.

Thus, from the above rules, auto will be deduced to the type of expr (after some type conversion stated above).

Note that, when an expression is a reference to T, it will be adjusted to T before prior analysis.

So whatever expr is – rvalue, lvalue, or lvalue/rvalue ref type – the type of auto will always be the type of expr without reference.

auto s1 = 1; //int
int &x = s1;
auto s2 = x; //int
int &&y = 2;
auto s3 = y; //int

Second, let's look at

auto &s = expr;

This will be same as

template<class T>
void f(T &s);

f(expr);

The extra rule from standard is as follows:

If P is a reference type, the type referred to by P is used for type deduction.

So the deduction of auto will be exactly same as without &, but after the auto type is deducted, the & is added to the end of auto.

//auto &s1 = 1; //auto is deducted to int, int &s1 = 1, error!
const auto &s1 = 1; //auto is deducted to int, const int &s1 = 1; ok!
const int &x = s1;
auto &s2 = x; //auto is int, int &s2 = x; ok!
int &&y = 2;
auto &s3 = y; //auto is int, int &s3 = y; ok! 

Note that the last y is an lvalue. The rule of C++ is: named rvalue reference is an lvalue.

Lastly:

auto &&s = expr;

This is no doubt same as

template<class T>
void f(T &&s);

f(expr);

One additional rule from standard:

If P is an rvalue reference to a cv-unqualified template parameter and the argument is an lvalue, the type “lvalue reference to A” is used in place of A for type deduction.

This actually says that, if expr is an rvalue, the rule will be same as the second case (lvalue case), but if expr is an lvalue, the type of A will be an lvalue reference to A.

Note from previous explained, A is never reference, because the type of an expression is never a reference. But for this special case (auto &&, and A is an lvalue), reference to A must be used, regardless expr itself is a reference type or not.

Example:

auto &&s1 = 1; //auto is deducted to int, int &&s1 = 1, ok!
int x = 1;
auto &&s2 = x; //x is lvalue, so A is int &, auto is deducted to int &, int & &&s2 = x; ok!
int &&y = 2;
auto &&s3 = y; //y is lvalue, auto is int &, int & &&s3 = y; ok!

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...