【发布时间】:2023-03-22 10:06:01
【问题描述】:
我意识到我可以通过using 为指向noexcept 函数的指针声明一个类型,但是如果我使用typedef,则禁止这样的声明。考虑下面的代码:
#include <iostream>
using fptr = void(*)() noexcept;
// typedef void(*FPTR)() noexcept; // fails to compile
void f() noexcept
{
std::cout << "void f() noexcept" << std::endl;
}
void g()
{
std::cout << "void g()" << std::endl;
throw 10;
}
int main()
{
fptr f1 = f;
fptr f2 = g; // why can we do this?
try {
f1();
f2();
}
catch (...)
{
std::cout << "Exception caught" << std::endl;
}
}
如果我取消注释 FPTR 声明,我会得到 p>
error: 'FPTR' declared with an exception specification
但是,using 工作正常。使用 gcc4.9 和 gcc5 编译。
我的问题是:
- 为什么会出现这种不一致?
- 为什么我们甚至可以使用
using和noexcept,因为我们可以将指针绑定到未声明noexcept的函数,如fptr f2 = g;行
好像是gcc相关的bug,连gcc5都没有捕捉到。填写错误报告
【问题讨论】:
-
“我意识到我可以通过 using 声明一个指向 noexcept 函数的指针” 不,你不能。那是一种类型,而不是指针。
-
@LightnessRacesinOrbit 是的,你是对的,这就是我的意思,更正它