gpt4 book ai didi

c++ - 为什么 typedef (typedef int* pt) 可以使 pt() 发生?

转载 作者:行者123 更新时间:2023-11-30 01:16:59 25 4
gpt4 key购买 nike

我写了以下代码:

typedef int* pt;
pt p = pt(); // compile ok

但是

int* p = (int*)()   // compile fail.

我想知道为什么?它们有什么区别?

最佳答案

首先,函数式转换符号中不允许使用额外的括号。例如

int i1 =  int ();  // compiles
int i2 = (int)(); // doesn't compile

但即使省略括号,您的示例也不会编译

int* p = int*();   // doesn't compile

这是因为函数式强制转换表示法要求类型名称是简单类型说明符类型名称说明符

§5.2.3 [expr.type.conv]

1   A simple-type-specifier (7.1.6.2) or typename-specifier (14.6) followed by a parenthesized expression-list constructs a value of the specified type given the expression list. ...
2   The expression T(), where T is a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv-qualified) void type, creates a prvalue of the specified type, whose value is that produced by value-initializing (8.5) an object of type T; no initialization is done for the void() case.

simple-type-specifier 基本上表示由单个单词组成的类型名称。由于同样的原因,下一行没有编译

unsigned int i = unsigned int(); // doesn't compile

关于c++ - 为什么 typedef (typedef int* pt) 可以使 pt() 发生?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25499183/

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