异常规范在声明和函数实现中不兼容 [英] Exception specifications are not compatible in declaration and in realisation of function
问题描述
我们有以下代码
int main()
{
void f() throw(int);
f();
return 0;
}
void f() { }
GCC clang编译好。但是,在标准中有这样的段落:
GCC and clang compiles it well. But, in standard there is such paragraph:
n3376 15.4 / 4
如果任何函数具有异常规范,它不是noexcept规范,允许
所有异常,该函数的所有声明,包括定义和任何显式特化,应具有兼容的异常规范。n3376 15.4/4
If any declaration of a function has an exception-specification that is not a noexcept-specification allowing all exceptions, all declarations, including the definition and any explicit specialization, of that function shall have a compatible exception-specification.
并且对于以下示例:gcc - error,clang - warning
And for following example: gcc - error, clang - warning
void f() throw(int); int main() { f(); return 0; } void f() { }
是这些片段的区别?谢谢。
Why there is difference in these snippets? Thanks.
推荐答案
从std规范的n3376 15.4 / 4,函数的所有dereclices和定义必须具有相同的投掷类型。这里:
The n3376 15.4/4 from the std specifie that all déclarations and definitions of a function must have the same throwing type. Here :
void f() throw(int); int main() { f(); return 0; } void f() { }
void f()throw(int);
和定义void f(){}
所以他们是冲突,因为声明是为一个函数,它抛出int,而定义是一个没有throw规范的函数。the declaration
void f() throw(int);
and the definitionvoid f() { }
are in global scop. So they are in conflict because the declaration is for a function which throw int while the definition is for a function without a throw specification.现在,当你把声明主要scop,定义不在同一个scop,在这个scop的定义是不知道所以你可以编译。
Now, when you put the declaration in the main scop, the definition isn't in the same scop, during this scop the definition isn't known so you can compile.
我希望你能理解我的英语,对不起。
I hope you understood my english, sorry about it.
这篇关于异常规范在声明和函数实现中不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!