具有默认参数构造的 noexcept 说明符

noexcept specifier with default arguments construction

以下面的示例代码为例:

void test(const Item& item = Item()) {
   ...
}

假设,一旦 item 被传递给函数,这就不会抛出。

问题是:函数应该标记为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 to point(1,4) and point(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