I'm confused by the following piece of code:
#include <iostream>
using namespace std;
int *foo()
{
//Operation
}
int main ()
{
auto int ret = foo();
}
I compiled the above code under GCC, but I got the following error:
error: two or more data types in declaration of 'ret'
auto int ret = foo();
But, if I remove int
type, like so:
auto ret = foo();
then it runs successfully.
auto
is a storage class and int
is a data type, then why do I get the error "two or more data types" in the first case?
auto: This is the default storage class for all the variables declared inside a function or a block. Hence, the keyword auto is rarely used while writing programs in C language. Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope).
The auto keyword directs the compiler to use the initialization expression of a declared variable, or lambda expression parameter, to deduce its type.
auto
is not a storage class. It used to be, before C++11. But it was completely useless, so the keyword was re-purposed to allow automatic type inference. So when you say:
auto int ret = foo();
You are basically declaring the object to have 2 types (or possibly the same type twice), and that is an error. And when you say:
auto ret = foo();
The type of ret
is determined by whatever the function foo
returns, which is int*
in this case.
auto
is a storage class
That used to be true before C++11 but not any more.
Starting from C++11, the meaning of the word has been changed. It is now used to automatically deduce types. See http://www.stroustrup.com/C++11FAQ.html#auto.
Why in first case I got an error "two or more data types"?
By using
auto int ret = foo();
you are trying to specify two types for ret
-- one deduced and the other explicitly specified.
If you want to use an explicitly specified type, you can use:
int ret = *foo(); // Since foo() returns a pointer.
or
int* ret = foo();
or you can let the compiler deduce the type by using:
auto ret = foo();
auto is NOT a storage class (not anymore since C++11) C++11 brings the keyword allowing the compiler to infer what is the type required for the variable you are declaring.
so basically doing auto int myVar is as invalid as string double myVar2 or bool long myVar3... the variable can have only one data type defining it, and in your case the keyword auto does that...
how to get rid off the error:
Remove the int type and just use auto, doing so will let the compiler to *AUTO***matically infer that type of the variable **ret is exactly that from what foo()
returns :) just genial!
auto ret = foo();
from the doc:
For variables, specifies that the type of the variable that is being declared will be automatically deduced from its initializer. For functions, specifies that the return type is a trailing return type or will be deduced from its return statements (since C++14). for non-type template parameters, specifies that the type will be deduced from the argument
You wrote that:
auto
is a storage class
but this is no more true in C++11 (or later). The auto
keyword has been reused for something completely different (some limited kind of type inference). The former C++03 or C99 auto
storage class is now (in C++11) always implicit and should not use the auto
keyword.
(if you like type inference, C++11 is not doing it very well, but C++14 or C++17 have progressed on it; Ocaml has a much more powerful and interesting Hindley-Milner type inference which is more "global"; that is why I wrote "some limited kind")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With