xcode 在 C++ 中更宽松的抛出说明符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2687208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
looser throw specifier for in C++
提问by MLS
I am getting an error that says:
我收到一条错误消息:
error: looser throw specifier for 'virtual CPLAT::CP_Window::~CP_Window()'
错误:“虚拟 CPLAT::CP_Window::~CP_Window()”的更宽松的抛出说明符
On the destructor, I have never heard of this before and some Google Searches say this might be a GCC 4 problem, which I would not be sure how to work around since I need GCC 4 to build a Universal Binary.
在析构函数上,我以前从未听说过这个,一些 Google 搜索说这可能是 GCC 4 问题,我不确定如何解决,因为我需要 GCC 4 来构建通用二进制文件。
My Environment: OS X 10.6, XCode 3.2.2, GCC 4 to build a universal binary.
我的环境:OS X 10.6、XCode 3.2.2、GCC 4 构建通用二进制文件。
What is the issue?
问题是什么?
回答by Jonathan M Davis
I assume that CPLAT has a base class? I'm also guessing that you did not put a throw specifier on CPLAT's destructor?
我假设 CPLAT 有一个基类?我也猜你没有在 CPLAT 的析构函数上放置一个 throw 说明符?
You can put throw(X)
(where X is a comma-separated list of exceptions) at the end of a function's signature to indicate what exceptions it's allowed to throw. If you put throw()
as the throw specifier, then it would indicate that no exceptions can be thrown from that function. It's fairly common to do this with destructors since you don't ever want a destructor to throw an exception.
您可以throw(X)
在函数签名的末尾放置(其中 X 是逗号分隔的异常列表)以指示允许抛出哪些异常。如果您将其throw()
作为 throw 说明符放置,则表示该函数不能抛出任何异常。使用析构函数执行此操作是很常见的,因为您永远不希望析构函数抛出异常。
A class that overrides a function which has a throw specifier cannot have a looser throw specifier (list more exceptions) than the function being overridden, since that would indicate that the derived class' function could violate the throw specifier of the base class' function. Not having a throw specifier means that any exception can be thrown from that function, so it's as loose as it can get.
覆盖具有 throw 说明符的函数的类不能具有比被覆盖的函数更宽松的 throw 说明符(列出更多异常),因为这表明派生类的函数可能违反基类函数的 throw 说明符。没有 throw 说明符意味着可以从该函数抛出任何异常,因此它尽可能松散。
In all likelihood, you need to add throw()
to the end of the function signature of CPLAT's destructor.
很有可能,您需要在throw()
CPLAT 的析构函数的函数签名的末尾添加。
Edit:By the way, I should probably add that you probably don't want to use throw specifiers (other than throw()
on destructors) without really knowing that that's what you want. Unlike Java's checked exceptions, they're not caught at compile-time but rather terminate your program at runtime if violated. So, it's best not to use them unless you know what you're doing.
编辑:顺便说一句,我可能应该补充一点,您可能不想throw()
在不知道这就是您想要的情况下使用 throw 说明符(析构函数除外)。与 Java 的已检查异常不同,它们不会在编译时捕获,而是在运行时在违反时终止您的程序。因此,除非您知道自己在做什么,否则最好不要使用它们。
回答by Edward Strange
http://www.agapow.net/programming/cpp/looser-throw-specifier
http://www.agapow.net/programming/cpp/looser-throw-specifier
Did you put throw() after the declaration of ~CP_Window() ?
你把 throw() 放在 ~CP_Window() 的声明之后了吗?
Top link in google search "looser throw specifier" BTW.
谷歌搜索“更宽松的投掷说明符”中的顶级链接 BTW。