4

How is auto implemented in C++11? I tried following and it works in C++11

auto a = 1;
// auto = double
auto b = 3.14159;
// auto = vector<wstring>::iterator
vector<wstring> myStrings;
auto c = myStrings.begin();
// auto = vector<vector<char> >::iterator
vector<vector<char> > myCharacterGrid;
auto d = myCharacterGrid.begin();
// auto = (int *)
auto e = new int[5];
// auto = double
auto f = floor(b);

I want to check how this can be achieved using plain C++

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
Avinash
  • 12,851
  • 32
  • 116
  • 186
  • 7
    It takes the type of the thing on the right and puts it on the left. – GManNickG Apr 18 '12 at 07:53
  • 1
    It takes the type the same way as decltype() and use it instead of auto – Jaffa Apr 18 '12 at 07:53
  • Do you really want to know how it is implemented? I'm pretty sure that depends on the compiler. – juanchopanza Apr 18 '12 at 07:55
  • Why didn't GManNickG and Geoffroy put your comments as answer? :) – Adrian Shum Apr 18 '12 at 07:57
  • 1
    @AdrianShum: I was being 50.111605% facetious. – GManNickG Apr 18 '12 at 07:59
  • 4
    "*I want to check how this can be achieved using plane C++*" This **is** plain C++, if your compiler is recent enough. I.e., it's standardized, and thus not anything special or out of the ordinary. – ildjarn Apr 18 '12 at 08:01
  • @GManNickG except there are cv qualification, value/reference considerations. – juanchopanza Apr 18 '12 at 08:02
  • @Geoffroy: auto and decltype are _not_ exactly the same, see http://stackoverflow.com/questions/6869888/the-relationship-between-auto-and-decltype – user396672 Apr 18 '12 at 08:05
  • Coincidentally Nawaz answered both questions. I would take his word on what `auto` is. – josephthomas Apr 18 '12 at 08:10
  • 3
    `I want to check how this can be achieved using plane C++` You can't "achieve" this with "plane C++"; it's a *language feature* of C++11, which C++03 lacks. I think Boost has some macro, but it's not quite as good. – Nicol Bolas Apr 18 '12 at 08:10
  • "plane C++"? auto is part of C++, so you can achieve the effect by using auto in C++. – PlasmaHH Apr 18 '12 at 09:12
  • 2
    I think you are confused by standards terms: C++11 is the current C++ standard. C++03 is the past C++ standard. If you now say `C++`, it is the same as if you say `C++11`. In ten years maybe, the past standard might be `C++11`, and the current one `C++18` or so, which you will _then_ call `C++`. – Sebastian Mach Apr 18 '12 at 09:55

4 Answers4

11

It does roughly the same thing as it would use for type deduction in a function template, so for example:

auto x = 1;

does kind of the same sort of thing as:

template <class T>
T function(T x) { return input; }

function(1);

The compiler has to figure out the type of the expression you pass as the parameter, and from it instantiate the function template with the appropriate type. If we start from this, then decltype is basically giving us what would be T in this template, and auto is giving us what would be x in this template.

I suspect the similarity to template type deduction made it much easier for the committee to accept auto and decltype into the language -- they basically just add new ways of accessing the type deduction that's already needed for function templates.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 2
    Not quite. `decltype(function_that_returns_reference())` is some `U&`, but `function(function_that_returns_reference())` would deduce `U`, just like `auto x = function_that_returns_reference()`. It's `auto` that gives us the `T` in the template. – R. Martinho Fernandes Apr 18 '12 at 09:43
  • @R.MartinhoFernandes: Yes, there are a few minor differences -- that's why I said "... **roughly** the same thing..." and "... **kind of** the same thing...". Templates precede `auto` by several years... – Jerry Coffin Apr 18 '12 at 14:04
7

In C++, every expression has value and type. For example, (4+5*2) is an expression which has value equal to 14 and type is int. So when you write this:

auto v = (4+5*2);

the compiler detects the type of the expression on the right side, and replaces auto with the detected type (with some exception, read comments), and it becomes:

int v = (4+5*2); //or simply : int v = 14;

Similarly,

auto b = 3.14159; //becomes double b = 3.14159;

auto e = new int[5]; //becomes int* e = new int[5];

and so on

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • So what is the type of v[i], where v is a standard library container? My understanding is that it is an lvalue since it returns by reference. But is the *type* of the expression a reference, or is the reference removed? This has a bearing on the behaviour of auto, since auto x = v[1] takes by value. – juanchopanza Apr 18 '12 at 09:15
  • 2
    @juanchopanza the type of `v[i]` in that case is some `T&` (or `T const&`). `auto x = v[i]` however, would deduce `T`, not `T&`. See here: http://stackoverflow.com/a/8797915/46642 – R. Martinho Fernandes Apr 18 '12 at 09:48
  • @R.MartinhoFernandes right, so then `auto` does something a bit more complicated than just detect the type of the expression. – juanchopanza Apr 18 '12 at 09:52
1

The auto keyword is simply a way to declare a variable while having it type being based upon the value.

So your

auto b = 3.14159;

Will know that b is a double.

For additional reading on auto take a look at the following references

C++ Little Wonders: The C++11 auto keyword redux

The C++0x auto keyword

josephthomas
  • 3,256
  • 15
  • 20
1

It works like before :)

Have you never hit a compiler error telling you:

error: invalid conversion from const char* to int

for such a code fragment: int i = "4";

Well, auto simply leverages the fact that the compiler knows the type of the expression on the right hand side of the = sign and reuses it to type the variable you declare.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722