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
-3
votes
1 answer

In whats case should use auto type?

I have some questions regarding when use auto Explicit, it is not more clear despite the context? Cases that are better be explicit instead of auto? And with lambda functions use auto how these auto f = [](auto v, auto x)…
user3854612
  • 127
  • 1
  • 1
  • 7
-3
votes
2 answers

auto keyword in c++4.3.2

My doubt is that does "auto" keyword works in C++4.3.2? I was writing a program to check the presence of a prefix in a word, I wrote something like this -- auto res = mismatch(prefix,word); And when I compiled it gave the error-- res was not…
gsdf
  • 275
  • 1
  • 6
  • 13
-3
votes
1 answer

Why does the order of the class members matter?

My compiler is GCC 4.9.0. struct A { int n; auto f() -> decltype(n) { // OK return n; } }; struct B { auto f() -> decltype(n) { // error: 'n' was not declared in this scope return n; } int n; }; int…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
-4
votes
2 answers

Why do i keep getting break error? I have the same problem with continue and pass

from webbot import Browser import time import pyautogui from pyautogui import * web = Browser() a == True web.go_to('http://au.yahoo.com//') web.scrolly(100) if a == True: try: web.click('Skip for now') …
-4
votes
2 answers

Access auto declared array

auto messwerte2 = { 3.5, 7.3, 4.9, 8.3, 4.4, 5.3, 3.8, 7.5 }; Which possibilities exist to access a single value explicitly of this array-like looking structure which as I was informed is actually a std::initializer_list ?
baxbear
  • 860
  • 3
  • 9
  • 27
-4
votes
4 answers

Keyword "auto" near critical points

Possible Duplicate: How much is too much with C++0x auto keyword I find using "auto" near critical points maybe cause some problems. This is the example code: #include #include #include using std::cout; using…
UniversE
  • 555
  • 2
  • 7
  • 25
-5
votes
1 answer

C++ STL Map gives TLE when looping with auto?

I was working on this problem on CodeForces. My solution was giving TLE and I could not figure out why. Eventually I narrowed it to the faulty line and it was essentially the following // map> res; for(auto z : res)…
Axo
  • 121
  • 3
-6
votes
1 answer

Would an break-less switch/case be feasible?

Would the suggestion to have a syntax for a switch that does not need breaks all the time feasible? My suggestion would be to insert auto both before switch and each case: auto switch(expr) { auto case 1: statement; statement; auto case 2:…
towi
  • 21,587
  • 28
  • 106
  • 187
-8
votes
1 answer

Why does this need auto?

auto data = new char[480][640][3](); char data = new char[480][640][3](); First works. Second doesnt. Why? Isn't auto supposed to just replace itself with the type of the initializer?
Moterius
  • 1
  • 1
1 2 3
84
85