14

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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
user2296177
  • 2,807
  • 1
  • 15
  • 26
  • 10
    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. – Peter Jul 09 '16 at 07:20

1 Answers1

12

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

Peter
  • 35,646
  • 4
  • 32
  • 74
  • 4
    `noexcept` doesn't affect only the function call. You're right that if a function cannot be called, the function call cannot be affected. But `noexcept` can in general make a difference even without calling anything, most clearly through the `noexcept` operator (`noexcept(f())` doesn't call `f()`). I think a good answer should also explain why none of the other cases apply (for instance, if all of those other cases only apply to callable functions, they don't apply to deleted functions), or if it's possible (which I suspect it isn't), give a counterexample. –  Jul 09 '16 at 07:54
  • 1
    Mentioning that you cannot overload on `noexcept` (unlike, say, `const`) would be helpful too. – Revolver_Ocelot Jul 09 '16 at 08:10
  • You're both right, hvd and Revolver_Ocelot. I've edited the answer to incorporate (or respond to) both your comments. The simple answer of "no" still applies, and the additional information you each point out supports that. – Peter Jul 09 '16 at 10:47