具有多词基本类型的“auto”变量声明会导致错误
`auto` variable declaration with multi-word fundamental types causes error
是否可以使用 auto 关键字和由两个或多个单词组成的类型名称来声明变量?
如果不是,为什么不呢?
例如
auto foo = unsigned int{0};
给出以下编译器输出
叮当声:
error: expected '(' for function-style cast or type construction
海湾合作委员会:
error: expected primary-expression before 'unsigned'
对于
auto foo = T{0};
要工作,T
必须是 简单类型说明符。
来自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.
如果你看到simple-type-specifier的定义,unsigned int
不是其中之一。
您可以使用以下任何一项:
auto foo = 0U;
auto foo = (unsigned int)0;
auto foo = static_cast<unsigned int>(0);
每当你希望编译器对多个事物进行分组但它没有时,你可以使用括号来强制它:
auto foo = (unsigned int)(0);
请注意,作为 @cpplearnern points out, if you use curly braces instead, it counts as a compound literal 并且不是合法的 C++,但在 C99 中是合法的:
auto foo = (unsigned int){0}; /* Not legal C++, though GCC and Clang support it. */
无论如何,更好的解决方案是使用 integral literals:
auto foo = 0u;
你要找的可能在这里 http://www.cplusplus.com/doc/tutorial/typecasting/(第 "Type casting" 部分)。
也许这样更好:https://en.cppreference.com/w/cpp/language/explicit_cast 在这个 link 下,您使用的是语法 (5):new_type { expression-list(optional) }
.
因此,如果您的类型名称中有一个 space,编译器将如何猜测 unsigned int {0}
是 (unsigned int) {0}
(可能是您的意思),还是 (unsigend (int {0}))
(这实际上是两次转换)...所以你必须明确地将类型名称放在括号中。
如果您想 long unsigned {x}
,这将很重要,因为那样我会得到
long
如果解释为long (unsigned {x})
unsigned long
如果解释为(long unsigned){x}
是否可以使用 auto 关键字和由两个或多个单词组成的类型名称来声明变量?
如果不是,为什么不呢?
例如
auto foo = unsigned int{0};
给出以下编译器输出
叮当声:
error: expected '(' for function-style cast or type construction
海湾合作委员会:
error: expected primary-expression before 'unsigned'
对于
auto foo = T{0};
要工作,T
必须是 简单类型说明符。
来自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.
如果你看到simple-type-specifier的定义,unsigned int
不是其中之一。
您可以使用以下任何一项:
auto foo = 0U;
auto foo = (unsigned int)0;
auto foo = static_cast<unsigned int>(0);
每当你希望编译器对多个事物进行分组但它没有时,你可以使用括号来强制它:
auto foo = (unsigned int)(0);
请注意,作为 @cpplearnern points out, if you use curly braces instead, it counts as a compound literal 并且不是合法的 C++,但在 C99 中是合法的:
auto foo = (unsigned int){0}; /* Not legal C++, though GCC and Clang support it. */
无论如何,更好的解决方案是使用 integral literals:
auto foo = 0u;
你要找的可能在这里 http://www.cplusplus.com/doc/tutorial/typecasting/(第 "Type casting" 部分)。
也许这样更好:https://en.cppreference.com/w/cpp/language/explicit_cast 在这个 link 下,您使用的是语法 (5):new_type { expression-list(optional) }
.
因此,如果您的类型名称中有一个 space,编译器将如何猜测 unsigned int {0}
是 (unsigned int) {0}
(可能是您的意思),还是 (unsigend (int {0}))
(这实际上是两次转换)...所以你必须明确地将类型名称放在括号中。
如果您想 long unsigned {x}
,这将很重要,因为那样我会得到
long
如果解释为long (unsigned {x})
unsigned long
如果解释为(long unsigned){x}