gpt4 book ai didi

重载函数的 C++ 异常规范

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:20:38 25 4
gpt4 key购买 nike

class ESClass
{
public:
void PrintMe() throw();
void PrintMe(int) throw(int);
};

我想知道我们是否可以为重载函数定义不同的异常规范。换句话说,我们能否对PrintMe函数的不同版本给出不同的异常规范?

Note from VS2010:

warning C4290: C++ exception specification ignored except to indicate a function is not __declspec(nothrow)

最佳答案

是的:它们是不同的函数,它们可以有不同的异常规范。

如果虚成员函数有异常规范,任何覆盖(不是重载)必须有至少与被覆盖的成员函数一样严格的异常规范.

当然,你应该"never write an exception specification"除了 those few situations where you must .

Visual C++ 不完全支持异常规范,因此它允许某些根据 C++ 语言规范实际上无效的代码。 The warning you mention只是意味着您使用的代码使用了 Visual C++ 不支持的 C++ 语言功能:

A function is declared using exception specification, which Visual C++ accepts but does not implement. Code with exception specifications that are ignored during compilation may need to be recompiled and linked to be reused in future versions supporting exception specifications.

关于重载函数的 C++ 异常规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7032050/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com