4

When marking special methods as =delete do the qualifiers of the method come into play ? In other words are:

inline constexpr myClass(const myClass&) noexcept = delete;

and

myClass(const myClass&) = delete;

equivalent ?

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
A.D
  • 427
  • 1
  • 4
  • 12

1 Answers1

2

As often it is, it's usually to just try it and ask the compiler:

class myClass {
    inline constexpr myClass(const myClass&) noexcept = delete;
    myClass(const myClass&) = delete;
};

int main() {
    return 0;
}


1 bla.cpp|4 col 5 error| ‘myClass::myClass(const myClass&)’ cannot be overloaded with ‘constexpr myClass::myClass(const myClass&)’
2 bla.cpp|3 col 22 error| note: previous declaration ‘constexpr myClass::myClass(const myClass&)

So yes, they are the same function. You can try

myClass x;
auto y = x;

with each to ensure the copy constructor was removed. This should make sense - the qualifiers are not a new declaration, they just qualify an existing one.

kabanus
  • 24,623
  • 6
  • 41
  • 74
  • So it would in fact be better (for clarity) to just write the second choice (just `myClass(const myClass&) = delete;`) I presume ? – A.D Feb 06 '21 at 14:42
  • 2
    @A.D that's an opinion, but I agree, for me it is. – kabanus Feb 06 '21 at 14:43
  • And so, when declaring methods `=default`, the qualifiers given have in a similar way no impact ? – A.D Feb 06 '21 at 14:45
  • @A.D Indeed. You just can't declare the same function or method twice with different qualifiers. – kabanus Feb 06 '21 at 14:46
  • But in the event I were to do ``` constexpr myClass(const myClass&) noexcept = default;``` would this add the constexpr qualifier to the default copy constructor ? Or would it simply be ignored in favor of the qualifiers of the default method ? – A.D Feb 06 '21 at 14:48
  • 1
    @A.D That's a different question, but yes. A bit harder to check that - declare a `constexpr` default constructor, copy constructor, and the two initialization statements in my test program. Miss one and the compiler will complain. Same deal - compiler knows best. – kabanus Feb 06 '21 at 14:53
  • Asking a compiler is rarely a good way to determining what is valid C++ code. You need instead to consult the language standard. – Cody Gray - on strike Feb 06 '21 at 16:33
  • @CodyGray sounds meticulous. I'm sure the question would benefit from your standard based answer. – kabanus Feb 06 '21 at 17:23