Take the following example code:
void test(const Item& item = Item()) {
...
}
Assume that, once item
has been passed to the function, this cannot throw.
The question is: the function should be marked noexcept
or noexcept(noexcept(Item()))
?
IHMO, the former should be ok, but I am not sure. 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.
In the standard [dcl.fct.default] states:
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.
Also there is a note (in [intro.execution] Program 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.