Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

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

Consider these two possible definitions for a class:

Exhibit A:

struct A
{
    A() = delete;
};

Exhibit A′:

struct A
{
    A() noexcept = delete;
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Edit:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...