将已删除的函数声明为noexcept有什么意义吗? [英] Is there any point in declaring a deleted function as noexcept?

查看:211
本文介绍了将已删除的函数声明为noexcept有什么意义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个类的以下两种可能的定义:

Consider these two possible definitions for a class:

展览A:

struct A
{
    A() = delete;
};

图A':

struct A
{
    A() noexcept = delete;
}

将已删除的函数声明为noexcept有什么意义吗?

Is there any point in declaring a deleted function as noexcept?

推荐答案

(最初将其发布为评论,但建议将其发布为答案.)

(Posted this initially as a comment, but encouraged to post as an answer.)

不,不.不能调用已删除的函数(或者在构造函数的情况下,该函数用于初始化对象),更不用说抛出异常了.

Simply, no. A function that is deleted cannot be called (or, in the case of a constructor, used to initialise an object) let alone throw an exception.

hvd在下面的注释中提到,noexcept(f())不调用f().如果类A的构造函数是delete d,则无论是否将该构造函数声明为noexceptnoexcept(A())都将无法编译. (本质上)这是要求为noexcept(expression)提供有效表达式的要求的结果-类A的表达式A()需要有效的构造函数.

hvd mentioned in comments below that noexcept(f()) does not call f(). If the constructor of class A is deleted, then noexcept(A()) will fail to compile, regardless of whether the constructor is declared noexcept. This is (essentially) a consequence of the requirement that noexcept(expression) be given a valid expression - and an expression A() for a class A requires a valid constructor.

Revolver_Ocelot还正确地指出不可能在noexcept上重载(即,不可能有两个具有相同签名的函数,除了一个是noexcept而不是一个).因此,在类A的定义中,A() = deleteA() noexcept = delete都具有相同的效果,即类A没有无参数构造函数.

Revolver_Ocelot also correctly points out that it is not possible to overload on noexcept (i.e. it is not possible to have two functions with the same signature, except that one is noexcept and one isn't). So, within a definition of class A, both A() = delete and A() noexcept = delete both have the same effect i.e. class A not having a non-argument constructor.

这篇关于将已删除的函数声明为noexcept有什么意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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