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?

8
  • 4
    It doesn't affect run-time performance at all. Oct 27, 2013 at 13:48
  • 17
    c and c++ are statically typed. All types must be known at compile-time. auto could not require run-time type checking, as it is part of c++.
    – Rubens
    Oct 27, 2013 at 13:50
  • 5
    @Rubens unless we deal with RTTI though..
    – vines
    Oct 27, 2013 at 13:51
  • 1
    @pranitkothari or dynamic_cast<>
    – user529758
    Oct 27, 2013 at 13:54
  • 2
    @pranitkothari yes, typeid and dynamic_cast<>.
    – vines
    Oct 27, 2013 at 13:55

5 Answers 5

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.

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
10
  • 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. Oct 28, 2013 at 10:50
  • 5
    and people were writing iterators incorrectly and/or avoiding them before auto. auto makes correct code easier to write Oct 30, 2013 at 5:58
  • 4
    Herb Sutter seems to think that it is ok to use auto for convenience purposes in the article "elements of modern c++ style"
    – daramarak
    Nov 4, 2013 at 8: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 4, 2013 at 18:33
  • 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, 2016 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;
1
  • 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. Oct 31, 2013 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.

3
  • 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, 2013 at 0: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.) Oct 23, 2017 at 15:52
  • Having slept a night over this I awoke in the morning and suddenly realized: I became an old-fashioned type-writer... Oct 24, 2017 at 5: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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.