gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:36:40 27 4
gpt4 key购买 nike

以下面的示例代码为例:

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 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

关于c++ - 具有默认参数构造的 noexcept 说明符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50273336/

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