'Does auto deduce the type at compile time or runtime in C++ 11?
Consider the following
auto a = 10;
When does the compiler know that a
is an int
, at compile time or at run-time? If it deduces the type at run-time, will it not affect the performance?
Solution 1:[1]
Compile time. In C++, runtime type information is stripped during compilation(without RTTI or virtual inheritance). It is not possible, in fact, to inspect the primitive type at runtime.
Solution 2:[2]
I just wanted to add some things that the other answers didn't address.
- every declaration must have a known type at compile time so
auto
doesn't get special treatment, it has to deduce the type at compile time. - You are sort of mis interpreting how
auto
should be used. Yes you can doauto i = 2;
and it works fine. But a situation where you need auto is a lambda for example. A lambda does not have a namable type (although you can assign it to anstd::function
). Another situation it is useful for is inside a class or function template it can be extremely difficult to figure out the type of certain operations (maybe sometimes impossible), for example when a function is called on a template type that function may return something different depending on the type given, with multiple types this can become essentially impossible to figure out which type it will return. You could of course just wrap the function in adecltype
to figure out the return but anauto
is much cleaner to write. - People also seem to use auto quite a bit for iterators because their types are quite a pain to write but I'm not so sure this is an intended primary use of
auto
Solution 3:[3]
It's done completely at compile time, with no performance difference.
auto i = 2;
compiles the same as
int i = 2;
Solution 4:[4]
The type of the variable declared auto
is done at compile time, which means if you have the following snippet of code:
auto i = 10; // i is an integer
i = 3.14; // i is still an integer, will truncate to 3
Herb Sutter (The guy currently in charge of the C++ standardization committee) recommends to "Use auto wherever possible. It is useful for two reasons. First, most obviously it’s a convenience that lets us avoid repeating a type name that we already stated and the compiler already knows. Second, it’s more than just a convenience when a type has an unknown or unutterable name, such as the type of most lambda functions, that you couldn’t otherwise spell easily or at all." (see this post on his blog). The intended use of auto
is to make things easier on the developer, so feel free to use it whenever it seems to fit.
Solution 5:[5]
C++ is a statically typed language. Types cannot be determined at runtime. The type of every expression must be known at compile time. Even when a variable is declared auto, its type is fixed at compile time.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Siyuan Ren |
Solution 2 | aaronman |
Solution 3 | vines |
Solution 4 | Sibren |
Solution 5 | Eslam Elkhair |