35

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137

5 Answers5

64

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.

Siyuan Ren
  • 7,573
  • 6
  • 47
  • 61
25

I just wanted to add some things that the other answers didn't address.

  1. 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.
  2. You are sort of mis interpreting how auto should be used. Yes you can do auto 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 an std::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 a decltype to figure out the return but an auto is much cleaner to write.
  3. 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
aaronman
  • 18,343
  • 7
  • 63
  • 78
  • 5
    :+1, I like your first and second point, but 3. code need to write while using iterator is clumsy, so it will defiantly make code more readable. – Pranit Kothari Oct 28 '13 at 10:50
  • @pranitkothari I wasn't saying you shouldn't do it I'm just saying it's not the reason why `auto` was added, people were using iterators long before it – aaronman Oct 28 '13 at 17:17
  • 5
    and people were writing iterators incorrectly and/or avoiding them before auto. auto makes correct code easier to write – Mooing Duck Oct 30 '13 at 05:58
  • @MooingDuck I use it for iterators too – aaronman Oct 30 '13 at 06:45
  • 4
    Herb Sutter seems to think that it is ok to use `auto` for convenience purposes [in the article "elements of modern c++ style"](http://herbsutter.com/elements-of-modern-c-style/) – daramarak Nov 04 '13 at 08:40
  • 3
    @daramarak not so sure I fully agree with that, why is `auto i = 2` a good thing. Let's say you wanted it to be a long and you wrote that. Then you call a function with an overload for long and an overload for int. It will select the int overload which is wrong, and no warning will ever be issued – aaronman Nov 04 '13 at 18:33
  • @aaronman Sutter explicitly says in the article "If you want to explicitly enforce a type conversion, that’s okay; state the target type. " which is just the case for the above case. Don't get me wrong, I see that the above case might cause problem even for seasoned programmer, but most cases are not like this, and in those cases it is just practical to use them, and even members of the c++ committee encourages the use of them. – daramarak Nov 05 '13 at 10:36
  • 1
    Nod to dramarak. Almost always use auto: http://herbsutter.com/2013/08/12/gotw-94-solution-aaa-style-almost-always-auto/ – erapert Nov 08 '13 at 17:26
  • Actually, the absurd inconvenience of using iterator types was one of the primary use cases of `auto`, being the first motivating example in one of the oldest proposals for it, back in 2003, long before lambdas were properly designed: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1478.pdf – Sebastian Redl Jan 31 '16 at 18:07
  • 2
    On the point of point 3, I'll quote Bjarne directly from his book, TC++PL4, "Using auto, we avoid redundancy and writing long type names. This is especially important in generic programming where the exact type of an object can be hard for the programmer to know and the type names can be quite long". This address both concerns, that is 1) The intended purpose, which is when the type is unknown, and 2) A side benefit which is to circumvent long type names. – Chad Mx May 10 '16 at 13:29
9

It's done completely at compile time, with no performance difference.

auto i = 2;

compiles the same as

int i = 2;
vines
  • 5,160
  • 1
  • 27
  • 49
  • 6
    In other examples there might be a performance difference. auto is guaranteed never to do a type conversion, so it is always at least as fast, if not faster. – Tim Seguine Oct 31 '13 at 18:03
8

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.

Sibren
  • 1,068
  • 11
  • 11
Rastaban
  • 881
  • 6
  • 8
  • 1
    There is a third situation you didn't describe: when the type changes but the code doesn't. This happens when we are developing functions for containers, since they frequently have similar or identical interfaces, so the code hardly changes, so, if we change the type, the code automatically adapts to those changes - no retyping. auto really helps developing in those situations. – Fred Oct 30 '13 at 00:17
  • I'm sure that Herb Sutter is "knowing what he does" concerning programming in C++. I'm convinced that the compiler deduces the correct type whenever I use `auto`. However, I'm not convinced that I'm always knowing what the compiler deduces and whether it matches completely my expectation. Therefore I prevent the use of `auto` whenever I'm able to. (I remember a time (though its decades ago) when explicity of source code was counted as modern and improvement.) – Scheff's Cat Oct 23 '17 at 15:52
  • Having slept a night over this I awoke in the morning and suddenly realized: I became an old-fashioned type-writer... – Scheff's Cat Oct 24 '17 at 05:25
2

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.