gpt4 book ai didi

c++ - 在编译指向可能抛出的函数的非抛出指向时,gcc 有什么问题吗?

转载 作者:行者123 更新时间:2023-11-27 22:42:50 25 4
gpt4 key购买 nike

我已阅读《C++ 入门》第 5 版这本书。在异常规范和指针、虚拟和复制控制部分,它说:

That is, if we declare a pointer that has a nonthrowing exception specification, we can use that pointer only to point to similarly qualified functions.

我还引用了 noexcept specifier (since C++11) ,还有类似的东西:

Pointers to non-throwing functions are implicitly convertible (since C++17)can be assigned (until C++17) to pointers to potentially-throwing functions, but not the other way around.

void ft(); // potentially-throwing
void (*fn)() noexcept = ft; // error

当我使用 gcc version 5.4.0 20160609 编译示例片段时,没有错误或警告。
但是,当我使用 Microsoft (R) C/C++ Optimizing Compiler Version 19.00.24215.1 for x86 编译它时,它会提示 error C2440: 'initializing': cannot convert from 'void (__cdecl * )(void)' 到 'void (__cdecl *)(void) noexcept'。这似乎是正确的行为。

所以我想知道 gcc 编译器有什么问题吗?

最佳答案

海湾合作委员会错误。这也不是有效的 C++11/14。 N3337 [except.spec]/5 ,强调我的:

If a virtual function has an exception-specification, all declarations, including the definition, of any function that overrides that virtual function in any derived class shall only allow exceptions that are allowed by the exception-specification of the base class virtual function. [ Example: ... — end example ] A similar restriction applies to assignment to and initialization of pointers to functions, pointers to member functions, and references to functions: the target entity shall allow at least the exceptions allowed by the source value in the assignment or initialization.

这就是人们说 C++17 之前的异常规范是“影子类型系统”时的意思:它们不是实际类型的一部分,而是在多个上下文中运行(初始化、赋值、虚拟覆盖)就好像他们是。

关于c++ - 在编译指向可能抛出的函数的非抛出指向时,gcc 有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46946506/

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