Is there any way that you can store data input from a user of unknown type into
an auto variable? It looks like I must immediately initialize the variable and there does not seem a way to do that without limiting the type of user input.
example:
auto strength;
std::cin >> strength;
this gives an error message:
error: declaration of 'auto strength' has no initializer|
I had a book that was talking about c++0x(c++11) and the auto keyword and the explanation seemed to imply you could input what ever into an auto variable and yet you must immediately initialize the variable. I was wondering if I was missing something and could not find any answer. Thanks for answering.
Some functions in the standard have an unspecified return type, meaning you cannot possibly know what the return type is in advance. You must use auto in these cases.
auto is also useful for when you're lazy and don't want to type the full type out and you know it's safe to use auto. This comes into play with some really crazy nested templates.
Sure it is. Let's say I want to make a sort algorithim by passing an stl contsiner to it. I would use a template for the unknown container type and auto for the iterator
Basically it deduces the type from a given value. Ex if you set an auto value equal to a double auto becomes double if you set auto even to a string it becomes a string value same with iterators and lambdas and templated stuff like that.
Basically the format is this
1 2
auto variable_name = value_of_a_certain_type;
//variable_name is now the type of the value
ex:
1 2 3 4
auto num = 1.2;
//num is now a double
auto str = "Hello";
//str is now a string
Sure it is. Let's say I want to make a sort algorithim by passing an stl contsiner to it. I would use a template for the unknown container type and auto for the iterator
You can use auto without templates and templates without auto.
i never said one is neccesary to use with the other. as shown above by your being too lazy to type out the type, and dischs example, templates arent required. i only said that ive typically seen auto used in those cases i mentioned
I was very confused and then I downloaded the sample code from the authors site(http://workbench.cadenhead.org/book/cpp-24-hours/source/chapter21/Combat.cpp) and saw the different contradictory type assignment for lines 6 to 8. So after a bit of frustration and swearing I moved on till I read one of the end of chapter activities:
Write a program with four overloaded square() functions that multiply a number by itself and take int, long, float, and double as their only parameter. Store the result of the function in an auto-typed variable and display it.
This made me wonder what the point of the above exercise was because I had already worked on overloaded functions and it seemed pretty annoying to do so much work just to store it in one auto variable.(What a challenge!) I started to lose confidence that there wasn't more to auto. I thought all you guys would surely be able to point out what I had missed. So far I do not believe I have missed anything and will probably toss the book aside.
This is the code I was reading in a book that said I must immediately initialize an auto variable:
auto uses the type of the initializer to determine the type of the variable to declare. If there is no initializer, there is no information available for it to use to determine the type the variable should be.