具有按值参数&的构造函数没事 [英] Constructor with by-value parameter & noexcept

查看:94
本文介绍了具有按值参数&的构造函数没事的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此示例代码中:

explicit MyClass(std::wstring text) noexcept;

此处正确使用noexcept吗? wstring可能会引发构造问题,但是引发抛出是在我们进入构造函数之前还是在我们进入构造函数时发生的吗?

Is the use of noexcept here correct? wstring can potentially throw on construction but does the throw happen before we are in the constructor or while we are in the constructor?

假设可以将其推广到任何带有按值参数的函数.

Suppose this can be generalised to any function taking a by-value parameter.

推荐答案

函数参数的构造和破坏发生在调用者的上下文中.因此,如果text的构造抛出异常,则不违反noexcept.

The construction and destruction of function parameters happens in the context of the caller. So no, if the construction of text throws, that is not a violation of noexcept.

不久后人们会发表评论并要求提供规格报价:)因此,我给您5.2.2p4

Soon folks would comment and ask for a spec quote :) So I will give you 5.2.2p4

每个参数的初始化和销毁​​发生在 调用函数.

The initialization and destruction of each parameter occurs within the context of the calling function.

这篇关于具有按值参数&的构造函数没事的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆