Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is `auto a(b);` parsed in C++? [duplicate]

Tags:

c++

I came across a curious line of code of the form:

// Given the following definitions:
class B;
B b;

// Line of interest:
auto a(b);

I thought it must be a typo, but after some experimentation found out that it works, and seems to always call the copy constructor of the type of b (even if you have other classes that can also have a matching constructor, and even if you additionally delete the copy constructor for the type of b).

I don't know the technical name of such a statement, so I'm not sure how to search for it in cppreference or StackOverflow. How does the compiler parse this type of statement in general, and where is it documented?

--

Re: duplicate marking. I don't see how they've addressed this construct. I already know auto uses template type deduction, that doesn't clarify anything in this case.

like image 591
Apollys supports Monica Avatar asked Sep 18 '19 01:09

Apollys supports Monica


People also ask

What is auto in C with example?

C auto – meaning, usage, examples in code. In the language C auto is a keyword for specifying a storage duration. When you create an auto variable it has an “automatic storage duration”. We call these objects “local variables”. In C, all variables in functions are local by default.

What is the difference between auto and int in C++?

If auto is used to declare multiple variables, the deduced types must match. For example, the declaration auto i = 0, d = 0.0; is ill-formed, while the declaration auto i = 0, *p = &i; is well-formed and the auto is deduced as int.

What is the difference between auto and decltype in C++?

2) decltype Keyword: It inspects the declared type of an entity or the type of an expression. ‘auto’ lets you declare a variable with a particular type whereas decltype lets you extract the type from the variable so decltype is sort of an operator that evaluates the type of passed expression.

What does the auto keyword DO in C++?

1) auto keyword: The auto keyword specifies that the type of the variable that is being declared will be automatically deducted from its initializer. In the case of functions, if their return type is auto then that will be evaluated by return type expression at runtime.


Video Answer


1 Answers

How is auto a(b); parsed in C++?

Depends on what b is. If b is a type, then this is a declaration of function with name a with deduced return type and a single argument of type b.

If b is not a type, then this defines a variable by the name a whose type is deduced from the initialiser.

where is it documented?

The authoritative documentation is the standard document. The standard sections [dcl.type.auto], [dcl.ambig.res], [dcl.fct], [dcl.init] should be relevant.

There are also websites that offer the documentation in (arguably) more approachable manner.

This should also be covered by recent (as in, anything since 2011) introductory C++ books.

and even if you additionally delete the copy constructor for the type of b

I doubt this. Create a mcve.

like image 76
eerorika Avatar answered Oct 08 '22 16:10

eerorika