以下面的示例代码为例:
void test(const Item& item = Item()) {
...
}
假设一旦 item
被传递给函数,this 就不能抛出。
问题是:函数应该标记为noexcept
还是noexcept(noexcept(Item()))
?
IHMO,前者应该没问题,但我不确定。非常感谢标准的引用!
最佳答案
默认参数是函数调用者的快捷符号。所以,当函数执行时,构造就已经完成了。
因此,noexcept
应该就足够了。
在 standard [dcl.fct.default] 状态:
If an initializer-clause is specified in a parameter-declaration this initializer-clause is used as a default argument. Default arguments will be used in calls where trailing arguments are missing.
Example: the declaration
void point(int = 3, int = 4);
declares a function that can be called with zero, one, or two arguments of type int. It can be called in any of these ways:point(1,2);
point(1);
point();
The last two calls are equivalent topoint(1,4)
andpoint(3,4)
, respectively.
还有一个注释(在[intro.execution]程序执行中):
Subexpressions involved in evaluating default arguments (8.3.6) are considered to be created in the expression that calls the function, not the expression that defines the default argument
关于c++ - 具有默认参数构造的 noexcept 说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50273336/