简体   繁体   English

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

[英]noexcept specifier with default arguments construction

Take the following example code: 请参考以下示例代码:

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

Assume that, once item has been passed to the function, this cannot throw. 假设一旦item已经传递给函数,就不能抛出。

The question is: the function should be marked noexcept or noexcept(noexcept(Item())) ? 问题是:函数应该标记为noexceptnoexcept(noexcept(Item()))

IHMO, the former should be ok, but I am not sure. IHMO,前者应该没事,但我不确定。 A quotation from the standard would be very appreciated! 非常感谢标准的报价!

Default arguments are shortcut notations for the caller of function. 默认参数是函数调用者的快捷方式。 So, when the function executes, the construction is already complete. 因此,当函数执行时,构造已经完成。

Thus, noexcept should be sufficient. 因此, noexcept就足够了。

In the standard [dcl.fct.default] states: 标准 [dcl.fct.default]中指出:

If an initializer-clause is specified in a parameter-declaration this initializer-clause is used as a default argument. 如果在参数声明中指定了initializer子句,则此initializer子句将用作默认参数。 Default arguments will be used in calls where trailing arguments are missing. 默认参数将用于缺少尾随参数的调用中。

Example: the declaration void point(int = 3, int = 4); 示例:声明void point(int = 3, int = 4); declares a function that can be called with zero, one, or two arguments of type int. 声明一个可以使用类型为int的零个,一个或两个参数调用的函数。 It can be called in any of these ways: point(1,2); 它可以通过以下任何一种方式调用: point(1,2); point(1); point(); The last two calls are equivalent to point(1,4) and point(3,4) , respectively. 最后两个调用分别相当于point(1,4)point(3,4)

Also there is a note (in [intro.execution] Program execution): 还有一个注释(在[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 参与计算默认参数(8.3.6)的Subexpressions被认为是在调用函数的表达式中创建的,而不是定义默认参数的表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM