1

I am getting confused between two concepts that is "auto" keyword that is introduced in C++11 and type casting (dynamic_cast/static_cast).

Does "auto" keyword in C++11 uses type casting internally?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
mrusom
  • 271
  • 2
  • 6
  • 1
    It uses template rules to determine the type. – wally May 24 '18 at 11:59
  • 4
    `auto` literally just "deduces" the type for you so you don't have to write it yourself, it never changes the type of a value by itself. It can only be used in places where the compiler can infer the type from the context ([more info here](https://en.cppreference.com/w/cpp/language/auto)). Type casting involves changing the type of the value, and you have to tell in your program what is the new type that you want to use the value as (more info [here](http://en.cppreference.com/w/c/language/cast) and [here](http://en.cppreference.com/w/cpp/language/explicit_cast)). – jdehesa May 24 '18 at 12:02
  • 1
    These are two different concepts. Casting is used when you want to treat a value of some type as a value of another type (e.g., treat an integer value as a floating-point value). Type deduction allows you to deduce type, for instance when you want to declare a variable being of type of its initializer expression `auto i = 1;`. – Daniel Langr May 24 '18 at 12:02
  • Possible duplicate of [C++ auto keyword. Why is it magic?](https://stackoverflow.com/questions/7576953/c-auto-keyword-why-is-it-magic) – Caleth May 24 '18 at 12:38
  • One of the advantages of `auto` is that by using it, you can be sure there won't be any implicit conversions when you initialize it. It's nearly the opposite of a cast. Perhaps you are confusing `auto` with [`std::any`](http://en.cppreference.com/w/cpp/utility/any)? – François Andrieux May 25 '18 at 02:17

1 Answers1

1

Let's keep it simple using an example

unsigned short s = 65535;
auto positive = s;
auto negative = (short) s;

std::cout << positive << std::endl; // prints 65535
std::cout << negative << std::endl; // prints -1

In this code:

  • In the first line you just initialize a unsigned short variable with value 655355
  • In the second line you initialize the variable positive and you let the compiler deduce its type from its initializer (see link). Therefore positive will be unsigned short because its initializer has that type.
  • In the third line, negative's type will be deduced as short, because you are casting the type of s from unsigned short to short.

Note that both positive and negative variables will hold the same value, which in hexadecimal is 0xffff, but they are interpreted in different ways due to their types.

So there's not such a thing as a difference between auto and casting as if they were comparable, they are different concepts.

  • auto will deduce the type or your variable based on certain rules
  • casting will change the type of your variable

I recommend you to read Effective Modern C++ by Scott Meyers to learn about how auto works.

rual93
  • 553
  • 4
  • 11
  • Maybe this is a bad place to ask, but is that book recommendation suitable for a beginner? I had put it on my reading list, but I was assuming it was more advanced. – Daniel Abercrombie May 25 '18 at 17:50
  • Well maybe you are right, but he gives a very good explanation about the type deduction. – rual93 May 31 '18 at 09:43