C++,函数指针异常错误

标签 c++ exception language-lawyer function-pointers c++17

我收到以下代码的“错误:不允许超出单一间接级别的异常规范”。请指出标准中不允许的部分。我想确定它确实是语言所必需的,或者只是编译器特定的错误。如果它来自语言规范,那么这条规则的动机是什么?我正在使用 clang 3.8.0。

  int main()
    {
        void (**fp)() throw() ;
    }

最佳答案

你说:

Please point me to a reference book /spec that says that it is not allowed. I want to be sure that it is really required by the language or just compiler specific error.

void (**fp)() throw() ;

您正在尝试在指向函数指针的指针声明中指定异常规范。这是标准不允许的。 异常规范仅允许用于一组有限的声明。

来自 https://timsong-cpp.github.io/cppwp/n3337/except.spec#2 (强调我的):

An exception-specification shall appear only on a function declarator for a function type, pointer to function type, reference to function type, or pointer to member function type that is the top-level type of a declaration or definition, or on such a type appearing as a parameter or return type in a function declarator. An exception-specification shall not appear in a typedef declaration or alias-declaration. [ Example:

 void f() throw(int);                    // OK
 void (*fp)() throw (int);               // OK
 void g(void pfa() throw(int));          // OK
 typedef int (*pf)() throw(int);         // ill-formed

end example ] A type denoted in an exception-specification shall not denote an incomplete type. A type denoted in an exception-specification shall not denote a pointer or reference to an incomplete type, other than void*, const void*, volatile void*, or const volatile void*. A type cv T, “array of T”, or “function returning T” denoted in an exception-specification is adjusted to type T, “pointer to T”, or “pointer to function returning T”, respectively.


你问:

If it is from language specification, what motivates this rule?

我没有答案。

关于C++,函数指针异常错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44377375/

相关文章:

c++ - pop_back() 是否真的使 std::vector 上的 *all* 迭代器无效?

android - 在android中上传文件到Dropbox,putFile方法抛出异常

c++ - Halide 中的递归 Func 调用

C++ 覆盖逻辑失败

c++ - 来自 exception::what() 的异常描述是否标准化为标准异常?

java.lang.RuntimeException : ERROR: Failed to recover corrupt cache entry 错误

c++ - typedef 和非简单类型说明符

c++ - 使用透明 std 函数对象时,我们还需要写空尖括号吗?

go - 为什么 "import"和ImportSpec之间可以出现多行注释,而PackageName和ImportPath之间不能?

c++ - 用于 linux C++ 的 HTTP 解析库