13

A lot of attention has been received by = default and = delete with respect to the special members (default constructor, destructor, copy and move). Can = default and = delete be used with other functions; member functions, free functions and operators etc.?

I can understand that = default would probably not be allowed outside the special members; since it is basically saying use the compiler generated default. The default would need to be clearly defined before the compiler is able to generate it. As far as I know, only the special members have these pre-defined defaults.

What about = delete; it is basically saying that the function is declared, but the implementation is explicitly not defined.

  • Can = delete be used with functions other than the special members?
  • What function types can it be used on (members, non-members, operators etc.)?
  • Or conversely, where (or when) is = delete prohibited from being used? Are there any restrictions on its use?
Niall
  • 30,036
  • 10
  • 99
  • 142
  • _"The function is declared, but the implementation is explicitly not defined"_ is the wrong way of thinking about `=delete`. The function is declared and is inaccessible; attempting to call the function is a compile-time error. The fact that the implementation is not defined is merely a consequence of this, because it wouldn't make sense to implement a function which can't be called. – Oktalist Sep 28 '14 at 22:17

1 Answers1

12

Any function whatsoever can be declared as deleted (using = delete). The standard does not impose any restrictions. This is covered by C++11[dcl.fct.def.delete]. It even gives an example of a deleted operator new.

Defaulted functions, on the other hand, are limited by [dcl.fct.def.default]§1 as follows:

1 ... A function that is explicitly defaulted shall

  • be a special member function,
  • have the same declared function type (except for possibly differing ref-qualifiers and except that in the case of a copy constructor or copy assignment operator, the parameter type may be “reference to non-const T”, where T is the name of the member function’s class) as if it had been implicitly declared, and
  • not have default arguments.

The "special member functions" referenced in the first bullet point are (as per [special]§1):

  • default constructor
  • copy constructor
  • move constructor
  • copy assignment operator
  • move assignment operator
  • destructor
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • I think the next question in this is how well this is supported in the compilers available today. Thanks for the additional references. – Niall Jul 10 '14 at 13:54
  • @Niall As far as I know, GCC and Clang support all of C++11 now (GCC since 4.8.1 or similar, Clang around that time as well). MSVC supports this since VS 2013, except for defaulting move functions. I don't know about other compilers; you can always consult their docs. – Angew is no longer proud of SO Jul 10 '14 at 13:58
  • Do all default-able members exist by default? Or do they have to be explicitly declared as `= default`? If so, why? – Snackoverflow Oct 21 '18 at 09:48
  • 1
    @anddero That's a separate question worth its own research, and might even be already answered somewhere here on SO. But in short: If you don't declare *any* of them, all of them will be defaulted. If you declare some but not all, there are rules which cover what gets defaulted and what gets deleted. – Angew is no longer proud of SO Oct 22 '18 at 07:05