Is C++11 auto keyword exactly defined for all cases? Or: how does auto know what I intend?

StackOverflow https://stackoverflow.com/questions/23411651

  •  13-07-2023
  •  | 
  •  

Domanda

Let's say, in C++11, I do

auto a = 4;

What will a be? An int (as I often read), an unsigned int, a short, a long, a size_t, a char? Is the behaviour of auto always defined, will it always be the exact same type (with the exact same bit length!) on each compiler and each architecture?

Another example:

class A{};
class B:A{};
auto x = new B();

Will x be of type *B or of type *A? Always the same on each compiler and platform? Both are perfectly legit, how does the compiler know which one I intend?

Is there an exact list of the auto behaviour?

È stato utile?

Soluzione

What will a be?

int, since that's the type of 4.

Will x be of type *B or of type *A?

B*, since that's the type of new B().

Is there an exact list of the auto behaviour?

Usually, it's the type of the initialiser; unless that's a reference type, in which case it's the underlying object type. There are a few other wrinkles for unusual types like arrays, as mentioned in the comments.

will it always be the exact same type (with the exact same bit length!) on each compiler and each architecture?

In most cases, the initialiser has a well-defined type, and that determines the type deduced by auto.

If the initialiser is an integer literal, then the type might depend on the platform; for example, 1000000 might be int on a 32-bit platform, but long on a 16-bit platform.

Altri suggerimenti

Every expression in C++ has a type. auto can only be used when there is an initialization expression. The type will be the type of that expression. The expression 4, for example, has type int, always, and the type of new B() is B*, always.

Of course, the fact that the type is clear to the compiler doesn't mean that it is clear to the reader. Abuse of auto is a good way of rendering a program unreadable, and also of making it fragile, since the compiler cannot check whether they type of the initialization expression is compatible with the desired type.

In the first case, a will be a int, see bellow:

auto a = 4 ; // int
auto b = 4U ; // unsigned int
auto c = 4L ; // long int
auto d = 4LLU ; // unsigned long long int, maybe it's ULL i don't remember...
auto x = 4.0 ; // double
auto y = 4.0f ; // float

In fact, there is a way to write any 'type of' int in C.

For new B(), well the compiler will take the only answer, which is B *.

auto match the type of the right value the variable is assigned to, without trying to infer anything, it's not its job!

You should not see the auto keyword as a magic stuff, but just something than may help you in case you don't want to have big declaration type.

The compiler doesn't know what you intend, and it doesn't care. The type of a or x is the type of the expression on the right hand side. Since the type of 4 is int, and the type of new B() is B*, it's the same as if you wrote int a = 4; B* x = new B();

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top