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

标签 c++ language-lawyer noexcept

以下面的示例代码为例:

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/

相关文章:

c++ - “Default member initializer needed within definition of enclosing class outside of member functions” - 我的代码格式不正确吗?

c++ - 模板参数包如何同时具有显式参数和推导参数?

c++ - 由于变体成员,N3690/N4140 与 N4659/N4727,隐式定义的构造函数被删除

c++ - d'tor 的函数尝试 block 是否应该允许处理抛出成员变量 d'tor?

c++ - Eigen LSCG 求解器性能问题

c++ - const 引用是否绑定(bind)到另一个从临时悬挂引用转换而来的引用?

c++ - 递归 noexcept() 是什么意思?

c++ - 将 System::Byte 的交错数组转换为无符号字符**

c++ - 在 C++ 中将父类类型转换为子类

c++ - 高效的数据包类型/传输协议(protocol)