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.

1055 questions
45
votes
4 answers

Is there a way to pass auto as an argument in C++?

Is there a way to pass auto as an argument to another function? int function(auto data) { //DOES something }
user3639557
  • 3,731
  • 4
  • 25
  • 50
43
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,040
  • 7
  • 16
42
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Јовић
  • 57,268
  • 38
  • 158
  • 253
38
votes
5 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,151
  • 5
  • 16
  • 26
36
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,228
  • 10
  • 49
  • 78
35
votes
5 answers

The relationship between auto and decltype

Is auto x = initializer; equivalent to decltype(initializer) x = initializer; or decltype((initializer)) x = initializer; or neither?
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
35
votes
5 answers

What is the type of an 'auto' return type when returning *this in an anonymous class?

In this code: struct { auto operator[](const char*) { return *this; } } m_some_class; What is type of auto in here?
Kate
  • 561
  • 1
  • 8
35
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
  • 466,055
  • 116
  • 851
  • 1,175
34
votes
2 answers

Does auto return type deduction force multiple functions to have the same return type?

Consider the below snippet: struct A { auto foo(), bar(); }; auto A::foo() { return 1; } auto A::bar() { return 'a'; } int main() { } It compiles fine in Clang++ 3.7.0. It fails in G++ 5.2.0: main.cpp: In member function 'auto…
Marc Andreson
  • 3,225
  • 5
  • 31
  • 51
34
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
  • 33,981
  • 13
  • 121
  • 269
34
votes
8 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
  • 4,967
  • 12
  • 39
  • 54
32
votes
3 answers

Correctly propagating a `decltype(auto)` variable from a function

(This is a follow-up from "Are there any realistic use cases for `decltype(auto)` variables?") Consider the following scenario - I want to pass a function f to another function invoke_log_return which will: Invoke f; Print something to…
Vittorio Romeo
  • 82,972
  • 25
  • 221
  • 369
31
votes
3 answers

Why does const auto &p{nullptr} work while auto *p{nullptr} doesn't in C++17?

This definition works: const auto &b{nullptr}; while this fails: auto *b{nullptr}; I have tried to compile this in Visual C++, GCC, and Clang. They all complain "cannot deduce type". In the second case, shouldn't b be deduced to have some type…
felix
  • 1,919
  • 5
  • 15
31
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 "var" keyword, from C#. So, what are differences of C# "var" and C++ "auto"?
Kangjun Heo
  • 773
  • 1
  • 5
  • 15
30
votes
1 answer

Why does decltype(auto) return a reference here?

I think (thought) I understand auto. Same about decltype. However, in C++14, one can have some diabolic thing like decltype(auto) as the return type of a function. Consider the following: decltype(auto) foo() { int m = 1; return m; } The…
vsoftco
  • 52,188
  • 7
  • 109
  • 221
1 2
3
70 71