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
117
votes
6 answers

The new keyword "auto"; When should it be used to declare a variable type?

Possible Duplicate: How much is too much with C++0x auto keyword Have we (as a community) had enough experience to determine when and/or whether auto is being abused? What I am really looking for is a best practices guide on when to use…
Martin York
  • 257,169
  • 86
  • 333
  • 562
114
votes
4 answers

Should the trailing return type syntax style become the default for new C++11 programs?

C++11 supports a new function syntax: auto func_name(int x, int y) -> int; Currently this function would be declared as: int func_name(int x, int y); The new style does not seem to be widely adopted yet (say in the gcc stl) However, should this…
mirk
  • 5,302
  • 3
  • 32
  • 49
103
votes
2 answers

Is 'auto const' and 'const auto' the same?

Is there a semantic difference between auto const and const auto, or do they mean the same thing?
steffen
  • 8,572
  • 11
  • 52
  • 90
87
votes
3 answers

Does 'auto' type assignments of a pointer in c++11 require '*'?

Given my variable being a pointer, if I assign it to a variable of "auto" type, do I specify the "*" ? std::vector *getVector(); //returns populated vector //... std::vector *myvector = getVector(); //assume has n items in…
Dolan Antenucci
  • 15,432
  • 17
  • 74
  • 100
85
votes
2 answers

C++11 auto: what if it gets a constant reference?

Please take a look at the following simple code: class Foo { public: Foo(){} ~Foo(){} Foo(const Foo&){} Foo& operator=(const Foo&) { return *this; } }; static Foo g_temp; const Foo& GetFoo() { return g_temp; } I tried to use auto like…
minjang
  • 8,860
  • 9
  • 42
  • 61
81
votes
11 answers

Using 'auto' type deduction - how to find out what type the compiler deduced?

How can I find out what type the compiler deduced when using the auto keyword? Example 1: Simpler auto tickTime = 0.001; Was this deduced as a float or a double? Example 2: More complex (and my present headache): typedef std::ratio<1, 1>…
kmiklas
  • 13,085
  • 22
  • 67
  • 103
80
votes
7 answers

Why do I need to explicitly write the 'auto' keyword?

I am moving towards C++11 from C++98 and have become familiar with the auto keyword. I was wondering why we need to explicitly declare auto if the compiler is able to automatically deduce the type. I know C++ is a strongly typed language and this is…
Farsan Rashid
  • 1,460
  • 4
  • 17
  • 28
76
votes
5 answers

A lambda's return type can be deduced by the return value, so why can't a function's?

#include int main(){ auto lambda = [] { return 7; }; std::cout << lambda() << '\n'; } This program compiles and prints 7. The return type of the lambda is deduced to the integer type based on the return value of…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
74
votes
4 answers

Advantages of auto in template parameters in C++17

What are the advantages of auto in template parameters that will (possibly) be introduced with C++17? Is it just a natural extension of auto when I want to instantiate template code? auto v1 = constant<5>; // v1 == 5, decltype(v1) is int auto…
Damian
  • 4,395
  • 4
  • 39
  • 67
74
votes
6 answers

decltype vs auto

As I understand it, both decltype and auto will attempt to figure out what the type of something is. If we define: int foo () { return 34; } Then both declarations are legal: auto x = foo(); cout << x << endl; decltype(foo()) y =…
James Leonard
  • 3,593
  • 5
  • 27
  • 32
70
votes
3 answers

Doesn't constraining the "auto" in C++ defeat the purpose of it?

In C++20, we are now able to constrain the auto keyword to only be of a specific type. So if I had some code that looked like the following without any constraints: auto something(){ return 1; } int main(){ const auto x = something(); return…
beep_boop
  • 819
  • 6
  • 15
70
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
  • 4,791
  • 6
  • 30
  • 55
70
votes
3 answers

C++11 Range-based for-loop efficiency "const auto &i" versus "auto i"

In C++11, I can iterate over some container like so: for(auto i : vec){ std::cout << i << std::endl; } But I know that this needlessly - needlessly, since I only need to print the values of vec - makes a copy of (EDIT: each element of) vec, so…
user2052561
  • 1,339
  • 2
  • 15
  • 18
68
votes
8 answers

How do I get a const_iterator using auto?

First question: is it possible to "force" a const_iterator using auto? For example: std::map usa; //...init usa auto city_it = usa.find("New York"); I just want to query, instead of changing anything pointed by city_it, so I'd…
virtualPN
  • 936
  • 1
  • 6
  • 8
64
votes
6 answers

C++11 auto declaration with and without pointer declarator

What's the difference between the types of bar1 and bar2? int foo = 10; auto bar1 = &foo; auto *bar2 = &foo; If both bar1 and bar2 are int*, does it makes sense to write the pointer declarator (*) in the bar2 declaration?
Henry Barker
  • 784
  • 1
  • 5
  • 9
1
2
3
84 85