Questions tagged [auto]

The `auto` keyword was repurposed in C++11 for a deduced type. When used to replace a type name in an initialized variable declaration, the variable is given the same type as the initializer. When used as a return type, the return type is specified as a trailing return type, or deduced from the return-expression.

Consider the following code:

bool Function()
{
    return true;
}

bool result = Function();

The return type of Function is unambiguously known at compile time, so the variable declaration can be replaced with:

auto result = Function();

The type of result will thus be deduced. The auto keyword becomes very useful, when type name is long:

for (std::vector<MyNamespace::MyType>::const_iterator iter = v.cbegin(); iter != v.cend(); iter++)

C++11 allows shorter declaration:

for (auto iter = v.cbegin(); iter != v.cend(); iter++)

It is worth noting that the keyword auto changed meaning with advent of C++11 and it is almost always used in this new context.

1270 questions
62
votes
1 answer

Why does auto x{3} deduce an initializer_list?

I love auto in C++11. It's wonderful. But it has one inconsistency that really gets on my nerves, because I trip over it all the time: int i = 3; // i is an int with value 3 int i = int{3}; // i is an int with value 3 int i(3); // i is…
Tristan Brindle
  • 16,281
  • 4
  • 39
  • 82
62
votes
3 answers

C++11 - declaring non-static data members as 'auto'

Does C++11 allow declaring non-static data members as 'auto' if they are initialized in the declaration? For example: struct S { auto x = 5; // in place of 'int x = 5;', which is definitely allowed }; GCC 4.7 rejects the above code, while it…
HighCommander4
  • 50,428
  • 24
  • 122
  • 194
59
votes
3 answers

Does a declaration using "auto" match an extern declaration that uses a concrete type specifier?

Consider the following program: extern int x; auto x = 42; int main() { } Clang 3.5 accepts it (live demo), GCC 4.9 and VS2013 do not (live demo for the former). Who is right, and where is the correct behavior specified in the C++ Standard?
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
48
votes
6 answers

How to iterate over a C++ STL map data structure using the 'auto' keyword?

So far I have always used an iterator for traversing through all the keys in an STL map as follows: for (std::map::iterator it=mymap.begin(); it!=mymap.end(); ++it){ std::cout << it->first << " => " << it->second << '\n'; …
KT100
  • 1,381
  • 5
  • 17
  • 27
47
votes
3 answers

Range-for-loops and std::vector

Why does this code work std::vector intVector(10); for(auto& i : intVector) std::cout << i; And this doesn't? std::vector boolVector(10); for(auto& i : boolVector) std::cout << i; In the latter case, I get an error error:…
Valentin
  • 1,108
  • 8
  • 18
46
votes
2 answers

Usage of auto in C++11

When I use auto to deduce a pointer type, I found a weird phenomenon. My code is like this: #include using namespace std; int main() { int i = 100; auto p1 = &i; auto *p2 = &i; cout << *p1 << " " << *p2 << endl; …
user5557984
44
votes
9 answers

Using auto in loops c++

I get warning signed/unsigned mismatch for the following code: auto n = a.size(); for (auto i = 0; i < n; i++) { } The problem is that by assigning 0 to i it becomes int rather than size_t. So what is better: size_t n = a.size(); for (size_t i = 0;…
user2381422
  • 5,645
  • 14
  • 42
  • 56
43
votes
4 answers

Using auto in a lambda function

#include #include void foo( int ) { } int main() { std::vector< int > v( { 1,2,3 } ); std::for_each( v.begin(), v.end(), []( auto it ) { foo( it+5 ); } ); } When compiled, the example above starts the error output like…
BЈовић
  • 62,405
  • 41
  • 173
  • 273
41
votes
1 answer

How to perfectly forward `auto&&` in a generic lambda?

C++14 supports generic lambdas. However, the following code is rejected by clang 3.4. #include void f(int); void f(int&); int main() { [](auto&& v) { f(std::forward(v)); }(8); // error } How to perfectly forward auto&& in a…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
39
votes
5 answers

Why is the 'auto' keyword useful for compiler writers in C?

I'm currently reading "Expert C Programming - Deep C Secrets", and just came across this: The storage class specifier auto is never needed. It is mostly meaningful to a compiler-writer making an entry in a symbol table — it says "this storage is…
Chi_Iroh
  • 1,061
  • 5
  • 14
39
votes
3 answers

Differences between C# "var" and C++ "auto"

I'm learning C++ now because I need to write some low level programs. When I learned about auto keyword, it reminds me of the var keyword from C#. So, what are the differences of C# var and C++ auto?
Kangjun Heo
  • 1,023
  • 1
  • 8
  • 19
38
votes
4 answers

Use of 'auto func(int)' before deduction of 'auto' in C++14

I have compiled following program in GCC using C++14. #include using namespace std; auto func(int i); int main() { auto ret = func(5); return 0; } auto func(int i) { if (i == 1) return i; else …
msc
  • 33,420
  • 29
  • 119
  • 214
37
votes
1 answer

What does auto&& do?

This is the code from C++11 Notes Sample by Scott Meyers, int x; auto&& a1 = x; // x is lvalue, so type of a1 is int& auto&& a2 = std::move(x); // std::move(x) is rvalue, so type of a2 is int&& I am having trouble understanding…
Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
37
votes
3 answers

Function Templates vs. Auto Keyword

Can the auto keyword in C++11 replace function templates and specializations? If yes, what are the advantages of using template functions and specializations over simply typing a function parameter as auto? template void myFunction(T…
Chunky Chunk
  • 16,553
  • 15
  • 84
  • 162
36
votes
1 answer

"auto" variable used in lambda in its own initializer

Today I found this code #include auto terminal = [](auto term) { return [=] (auto func) { return terminal(func(term)); …
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
1 2
3
84 85