`auto` variable declaration with multi-word fundamental types causes error

auto type specifier
when to use auto c
c++ auto type performance
swift declare variable without type
never use auto c
c++ auto iterator
c++ auto reference
swift declare variable without value

Is it possible to declare a variable with the auto keyword and a type name that is made of two or more words?

And if not, why not?

For example

auto foo = unsigned int{0};

Give the following compiler output

Clang:

error: expected '(' for function-style cast or type construction

GCC:

error: expected primary-expression before 'unsigned'

For

auto foo = T{0};

to work, T has to be a simple-type-specifier.

From the C++11 Standard/5.2.3 Explicit type conversion (functional notation)/3

Similarly, a simple-type-specifier or typename-specifier followed by a braced-init-list creates a temporary object of the specified type direct-list-initialized ([dcl.init.list]) with the specified braced-init-list, and its value is that temporary object as a prvalue.

If you see the definition of simple-type-specifier, unsigned int is not one of them.

You can use any of the following:

auto foo = 0U;
auto foo = (unsigned int)0;
auto foo = static_cast<unsigned int>(0);

C++ 11 Auto: How to use and avoid abuse, `auto` variable declaration with multi-word fundamental types causes foo = unsigned int{0}; Give the following compiler output Clang: error:  A- An array is a group of variables that all have the same type. B- Elements are located by index or subscript. C- The length of an array c is determined by the expression c.Length.

Whenever you want the compiler to group multiple things but it doesn't, you use parentheses to force it:

auto foo = (unsigned int)(0);

Note that as @cpplearnern points out, if you use curly braces instead, it counts as a compound literal and is not legal C++, though is legal C99:

auto foo = (unsigned int){0}; /* Not legal C++, though GCC and Clang support it. */

In any case, a better solution is to use integral literals:

auto foo = 0u;

C Programming Course Notes - Functions, Recipes to boost code readability and robustness with C++ 11 Auto Auto keyword simply tells the compiler to deduce the type of a This enables you to use the lambda expression multiple times in The problem was caused by the fact that the uninitialized variable Final words on C++ 11 Auto. When you declare a variable in your code, you must either specify its type explicitly, or use the auto keyword to instruct the compiler to deduce the type from the initializer. When you declare a function in your code, you must specify the type of each argument and its return value, or void if no value is returned by the function.

Declarations, It is also common to need the same code in multiple different programs. This can cause problems if the function does not in fact return an int. Declaring the variables just before you use them keeps the declaration and use on the A function with a void return type will not have a return value after the return statement. Adopting a consistent use of auto to declare your variable types will most likely improve your codes correctness, portability, efficiency, and most of all readability. If you have previously considered auto , and decided that it wasn't for you, take a moment to reconsider.

C Programming/Variables, The basic form imports the entire module; it consists of the import keyword A variable declaration introduces a variable named value into your program Functions can return multiple values using a tuple type as the return type of the function. Nonreturning functions and methods either cause an irrecoverable error or  If the placeholder type specifier is used to declare multiple variables, the deduced types must match. For example, the declaration auto i = 0, d = 0.0; is ill-formed, while the declaration auto i = 0, * p = & i; is well-formed and the auto is deduced as int. in the type-id of a new expression. The type is deduced from the initializer.

6.14, Within the C programming language, when managing and working with variables​, it is important to know the type of variables and the size of these types. Variants are different, but similar. I don't know pascal, but at least in C++ that's the case. Like `auto`, `std::variant` can have different types. Though, you have to specify a list of types that it can potentially hold, and it can change types at any point. Unlike `auto` variables, which cannot change types, their type is deduced at compile

Google C++ Style Guide, When initializing a variable, the auto keyword can be used in place of the type While using auto in place of fundamental data types only saves a few (if any) what type the function will return, which can lead to inadvertent errors. to declare functions using a trailing return syntax, where the return type is  Yes, you can use auto in a function declaration in C++14 (see example bellow). Though I suspect it's some sort of templating shorthand. Note the addresses of the &#039;i&#039; static variables are different between the Call that takes a functor and one that

Comments
  • Why would you do something like this?? The whole idea of auto is not to write the type. If you want to declare something (for the first time, with type and all) you use the normal declaration unsigned int foo = 0;. If you follow this with an auto bar = foo; all should be good.
  • @Emil Vatai I agree, but is there a good reason why this isn't allowed when it works with other fundamental types? Is it too complex to parse? The question still stands even if it isn't 'best practice'.
  • No, this is very far from "best practice". What do you mean by "other fundamental types?" Try deleting int (the meaning/semantics of the program wont change because unsigned is the same as unsigned int) and see if it compiles. That should get you closer to the answer.
  • Looking at the 'Fundamental Types' page on cppreference.com. Something like long double wouldn't work in this situation, you could use the L suffix, or a cast, but I'm wondering why exactly is this syntax not allowed.
  • Ahh... ok, by mult-word you ment a type which described by multiple words... (I thought a type with size larger than a register). So, yeah, the parsing is the problem. What you are doing here is casting. Let me look up some official documentation.
  • You can, but you shouldn't!
  • @EmilVatai auto foo = 0U seems clean. Why not?
  • Yeah, that's the "most reasonable", but still I'd say you shouldn't do it. If you want a variable foo of type unsigned int with value 0, the recommended way to go is unsigned int foo = 0;. The question was more with regard to why does this syntax work with types int (described by a single keyword) and not with unsigned int (described with multiple keywords).
  • @EmilVatai, thanks for the prod. I was able to find the references in the standard that explainswhy the OP's construct is wrong.
  • This is technically non-standard and compilers will warn you.
  • @cpplearner thanks for the feedback. Added that to the answer.