sách gpt4 ăn đã đi

c++ - sizeof 和函数模板 : sizeof(&f) vs sizeof(&f)

In lại 作者:太空狗 更新时间:2023-10-29 21:36:46 28 4
mua khóa gpt4 giày nike

giống [5.3.3/3] 所述(expr.sizeof,工作草案):

The sizeof operator can be applied to a pointer to a function, but shall not be applied directly to a function.

以下最小的工作示例可以正常编译:

void f() { }

int chính() {
sizeof(&f);
}

我希望下面的那个也能工作:

mẫu
void f() { }

int chính() {
sizeof(&f);
}

无论如何,即使它用 clang (v3.8) 编译,它也不使用 GCC (v6.1)。
错误是:

error: address of overloaded function with no contextual type information

我怀疑是GCC的bug(如果确定我会开工票)
我是对的还是我在这里遗漏了一些东西,而 GCC 确实是对的?


与此同时,我打开了 an issue到海湾合作委员会。

câu trả lời hay nhất

这是一个错误。以下代码编译正常:

mẫu
void f() { }

int chính() {
auto fptr = &f;
return sizeof(fptr);
}

请注意,起初我没有仔细阅读问题。我的印象是函数 f确实重载了,例如:

mẫu
void f() { }

mẫu
void f(T) { }

int chính() {
sizeof(&f);
}

在这样的阅读问题下,我准备了以下答案,我仍然想与社区分享:


我不会将其定性为错误。

将其定性为错误的论点如下 - 所有函数指针都具有相同的大小,那么为什么 kích thước của 中的函数很重要呢?运营商?

我要打败那个论点。

首先,它是从错误的前提出发的。 C++ 标准只保证

converting a prvalue of type “pointer to T1” to the type “pointer to T2” (where T1T2 are function types) and back to its original type yields the original pointer value

这并不一定意味着指向不同类型函数的指针的大小是相同的。然而,我承认在实践中这是真的。

接下来,即使我们接受了论证背后的前提和逻辑,那么我们也必须接受下面的程序也应该毫无问题地编译的说法:

mẫu
void f() { }

mẫu
void f(T) { }

int chính() {
auto fptr = &f;
return sizeof(fptr);
// fptr is not used anywhere else, so the compiler must not
// whine about the ambiguity on its declaration line
}

继续以这种方式,我们认为,只要后续代码消除了编译歧义,就不应报告编译歧义。

关于c++ - sizeof 和函数模板 : sizeof(&f) vs sizeof(&f),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39319745/

28 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress