gpt4 book ai didi

c++ - 使用简单类型说明符的显式类型转换(功能符号)

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:23:28 25 4
gpt4 key购买 nike

移植一些代码我发现了那一行

unsigned char uc = unsigned char(c);

被 MSVC 接受但被 GCC 拒绝。这个语法正确吗?
标准说

A simple-type-specifier (7.1.7.2) ... followed by a parenthesized optional expressionlist or by a braced-init-list (the initializer) constructs a value of the specified type given the initializer

是不是意味着MS是对的? unsigned char 是“简单类型说明符”吗?

最佳答案

GCCCLANG是正确的,代码无效。

Simple type specifier是单字类型名称:

The simple type specifiers are

simple-type-specifier:
nested-name-specifieropt type-name
nested-name-specifier template simple-template-id
nested-name-specifieropt template-name
char
char16_t
char32_t
wchar_t
bool
short
int
long
signed
unsigned
float
double
void
auto
decltype-specifier

type-name:
class-name
enum-name
typedef-name
simple-template-id

decltype-specifier:
decltype ( expression )
decltype ( auto )

unsigned char 不是简单类型说明符,它是简单类型说明符的组合,如Table 9 所示。从标准。

Table [tab:simple.type.specifiers] summarizes the valid combinations of simple-type-specifiers and the types they specify.

Table 9 — simple-type-specifiers and the types they specify

Specifier(s)  Type
...
unsigned char “unsigned char”
...

Here来自 cppreference.com 的解释:

2) The functional cast expression consists of a simple type specifier or a typedef specifier (in other words, a single-word type name: unsigned int(expression) or int*(expression) are not valid), followed by a single expression in parentheses.

关于c++ - 使用简单类型说明符的显式类型转换(功能符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38977808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com