【问题标题】:C++ method declaration with throw(...)带有 throw(...) 的 C++ 方法声明
【发布时间】:2014-06-30 10:59:56
【问题描述】:

我对 C++ 中的声明类方法有疑问。我通常使用声明方法而不提供 throw(会抛出任何东西)。但我在某处看到这样的声明:

void method(int param) throw (...);

这有什么意义吗?有什么区别?

【问题讨论】:

  • 你的意思是 throw (...) 字面意思吗?还是throw (something here)?如果是后者,那么这是 stackoverflow.com/questions/1589459/… 的副本(它出现在“相关”列表的顶部,就像您在写问题时所做的那样)。
  • 我的意思是 throw(...) 字面意思。

标签: c++ methods throw


【解决方案1】:

好吧it's not valid C++ 所以,不,它没有“任何意义”:

g++ -std=c++11 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp:1:31: error: expected type-specifier before '...' token
 void method(int param) throw (...);

(nor in C++03)

您可以在异常说明符中写入... 的唯一位置是在 dynamic-exception-specification 中的 type-id 之后,以形成包扩展([C++11: 15.4/16]), like so:

template <typename ...T>
void method(int param) throw (T...) {}

int main()
{
    method<int, bool>(42);
    // ^ somewhat like invoking a `void method(int) throw(int, bool)`
}

【讨论】:

    【解决方案2】:

    这是微软的扩展,基本意思是“这个函数可能会抛出一些东西”,相当于完全没有规范。添加它的价值值得怀疑。

    【讨论】:

    • 为了完整性:它也适用于我的 Clang 3.5,但不适用于 g++ -fms-extensions
    猜你喜欢
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 2014-03-24
    • 2011-07-09
    • 2010-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多