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

Displaying addresses using range-based loops in C++

Why doesn't the range-based loop with auto display addresses? The for loop: for (int i = 0; i < s; i++) cout << &ar[i] << endl; works normally, but range-based loop with auto doesn't: #include #include using namespace…
-2
votes
3 answers

C++ return different objects

i have a big problem.. I wonna select the Storage Service via a wrapper class. The returning value must be an object within the storage service class. I pasted my current approach. But my mindset didn't worked so far. Error: error: inconsistent…
N. Men
  • 3
  • 6
-2
votes
2 answers

Can you use auto with a comma

Can you use an auto deduced type where you have a comma to indicate initialization of two or more variables. Like this: auto p = c.begin(), e = c.end(); Or is the presence of two initializations (potentially) too confusing for a compiler? What…
Raedwald
  • 46,613
  • 43
  • 151
  • 237
-2
votes
1 answer

C++ Return Auto Array

Visual Studio shows me a error on auto**. Why? My code: auto** getMetaInfo(SQLHANDLE sqlStmtHandle) { SQLCHAR colName[256]; SQLSMALLINT colNameLen; SQLSMALLINT dataType; SQLSMALLINT numDecimalDigits; SQLSMALLINT…
-2
votes
1 answer

Why does top()'s return value change after calling pop()?

The const reference returned by priority_queue's top() changes after calling pop() (visual studio 2015) priority_queue queue; queue.push(1); queue.push(2); queue.push(3); const int & m = queue.top(); cout << m << endl; // 3 queue.pop(); cout…
stanleyerror
  • 728
  • 1
  • 9
  • 23
-2
votes
1 answer

Does std::tuple accept auto as a type

I have to return an int and a function object to the caller, I was thought of returning a tuple like make_tuple(int,[](some){}) I am now on GCC that doesn't support decltpe(auto) as a return type, is ther any way i could make my return type as…
RaGa__M
  • 2,550
  • 1
  • 23
  • 44
-2
votes
1 answer

Rule of thumb for using auto&& and const auto &

I have studied auto and I know it deduces types from initialized values. If I tried to write some guidelines for usage of auto, can I put below statements as rules? Use auto && for all values (r-values and l-value, as it is an universal ref) that…
gaurav bharadwaj
  • 1,669
  • 1
  • 12
  • 29
-2
votes
1 answer

Adding the two integers but one declared as an "int" and other as "auto"?

I am trying to find the sum of all the elements in an array, and declaring my initial accumulator variable sum as 0 using auto. #include #include #include #include #include using namespace std; int…
-2
votes
2 answers

typename in dependent scope

Below is a condensed version of my code that gives me a compiler error. The compiler tells me to put typename in front of 'std::deque::reverse_iterator', which makes sense. But if I do I receive the error at the bottom. What does it mean? How can it…
-2
votes
1 answer

Why doesn't this vector print?

The compile error says it was not declared in this scope, it does not name a type, expected ; #include #include using namespace std; int main() { vector vec; vec.push_back(1); vec.push_back(3); …
Sal Rosa
  • 551
  • 2
  • 8
  • 12
-3
votes
1 answer

Go html/template & escaping

Based on the example in the html/template documentation I can't say I fully understand why it appears that less and greater than are inconsistently escaped in my experiment: https://golang.org/pkg/html/template/#hdr-Introduction Does this warrant a…
-3
votes
5 answers

Is it possible to declare auto variables with an if?

My Code is below. struct conv{ struct des { des(int a) {} des(int a, int b) {} des(int a, int b, int c) {} }; }; int main(int argc, const char * argv[]) { int a = 1; int b = 1; int c = 1; if (a > 1) { auto…
冯剑龙
  • 569
  • 8
  • 22
-3
votes
1 answer

Ext JS 2.3 - Previous values in fields displaying- it's like an browser auto fill selections but different

i clicked on Destination Zip field and a list appeared which is not expected. i used autoComplete:False but still not working.Need Help.
Shobhit
  • 23
  • 6
-3
votes
1 answer

Fastest way to write a post and auto sync it with 1000 sites

I'm writing WordPress plugin which can post to many sites at once. When I publish a post or create a user or category on server, all content will be sync with clients by using form post method. But the code run too slowly. How can I speed up…
-3
votes
2 answers

c++ auto cin example

I am getting error like /hackerearth/CPP14_28/s_e3.cpp: In function ‘int main()’: /hackerearth/CPP14_28/s_e3.cpp:6:10: error: declaration of ‘auto x’ has no initializer auto x; ^ My code is , #include using namespace std; int…
Kamesh S
  • 143
  • 2
  • 8
1 2 3
84
85