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
236
votes
4 answers

Can the use of C++11's 'auto' improve performance?

I can see why the auto type in C++11 improves correctness and maintainability. I've read that it can also improve performance (Almost Always Auto by Herb Sutter), but I miss a good explanation. How can auto improve performance? Can anyone give an…
DaBrain
  • 2,889
  • 2
  • 17
  • 28
233
votes
14 answers

How much is too much with C++11 auto keyword?

I've been using the new auto keyword available in the C++11 standard for complicated templated types which is what I believe it was designed for. But I'm also using it for things like: auto foo = std::make_shared(); And more skeptically…
Alan Turing
  • 11,403
  • 14
  • 66
  • 114
186
votes
4 answers

What does auto&& tell us?

If you read code like auto&& var = foo(); where foo is any function returning by value of type T. Then var is an lvalue of type rvalue reference to T. But what does this imply for var? Does it mean, we are allowed to steal the resources of var? Are…
MWid
  • 3,809
  • 3
  • 17
  • 20
169
votes
8 answers

C++ auto keyword. Why is it magic?

From all the material I used to learn C++, auto has always been a weird storage duration specifier that didn't serve any purpose. But just recently, I encountered code that used it as a type name in and of itself. Out of curiosity I tried it, and…
Anne Quinn
  • 10,856
  • 7
  • 40
  • 83
168
votes
2 answers

What are some uses of decltype(auto)?

In c++14 the decltype(auto) idiom is introduced. Typically its use is to allow auto declarations to use the decltype rules on the given expression. Searching for examples of "good" usage of the idiom I can only think of things like the following…
Nikos Athanasiou
  • 24,831
  • 11
  • 72
  • 136
158
votes
7 answers

What is the type of lambda when deduced with "auto" in C++11?

I had a perception that, type of a lambda is a function pointer. When I performed following test, I found it to be wrong (demo). #define LAMBDA [] (int i) -> long { return 0; } int main () { long (*pFptr)(int) = LAMBDA; // ok auto pAuto =…
iammilind
  • 62,239
  • 27
  • 150
  • 297
154
votes
2 answers

arrow operator (->) in function heading

I came across the following code: template auto compose(T a, T1 b) -> decltype(a + b) { return a+b; } There is one thing I cannot understand: Where could I find out what does the arrow operator (->) mean in function…
user1234567
  • 2,779
  • 2
  • 16
  • 20
147
votes
14 answers

Is there a downside to declaring variables with auto in C++?

It seems that auto was a fairly significant feature to be added in C++11 that seems to follow a lot of the newer languages. As with a language like Python, I have not seen any explicit variable declaration (I am not sure if it is possible using…
DxAlpha
  • 1,363
  • 2
  • 7
  • 13
144
votes
4 answers

Why can I use auto on a private type?

I was somehow surprised that the following code compiles and runs (vc2012 & gcc4.7.2) class Foo { struct Bar { int i; }; public: Bar Baz() { return Bar(); } }; int main() { Foo f; // Foo::Bar b = f.Baz(); // error auto b =…
hansmaad
  • 16,551
  • 7
  • 46
  • 89
128
votes
6 answers

Is there auto type inferring in Java?

Is there an auto variable type in Java like you have in C++? An example: for ( auto var : object_array) std::cout << var << std::endl; for( auto var : object_array) var.do_something_that_only_this_particular_obj_can_do(); I know that there…
Games Brainiac
  • 71,327
  • 31
  • 126
  • 185
127
votes
6 answers

Lambda returning itself: is this legal?

Consider this fairly useless program: #include int main(int argc, char* argv[]) { int a = 5; auto it = [&](auto self) { return [&](auto b) { std::cout << (a + b) << std::endl; return self(self); }; }; …
n. 'pronouns' m.
  • 95,181
  • 13
  • 111
  • 206
127
votes
7 answers

Why does auto a=1; compile in C?

The code: int main(void) { auto a=1; return 0; } gets compiled without errors by the MS Visual Studio 2012 compiler, when the file has the .c extension. I have always thought that when you use the .c extension, compilation should be…
lee77
  • 1,463
  • 3
  • 10
  • 13
120
votes
3 answers

How does generic lambda work in C++14?

How does generic lambda work (auto keyword as an argument type) in C++14 standard? Is it based on C++ templates where for each different argument type compiler generates a new function with the same body but replaced types (compile-time…
sasha.sochka
  • 12,899
  • 8
  • 41
  • 64
103
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
  • 234,851
  • 74
  • 306
  • 532
97
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
  • 4,665
  • 2
  • 28
  • 44
1
2 3
70 71