可以删除非特殊C ++成员函数(或默认)吗? [英] Can non-special C++ member functions be deleted (or defaulted)?
问题描述
对于特殊成员 = default
和 = delete
已收到很多关注构造函数,析构函数,复制和移动)。可以与其他函数一起使用 = default
和 = delete
成员函数,自由函数和运算符等。
我可以理解, = default
特别成员;因为它基本上是说使用编译器生成的默认。默认值需要在编译器能够生成之前进行明确定义。据我所知,只有特殊成员有这些预定义的默认值。
= delete
- 可以<$>
- 可以在(成员,非成员,运算符)上使用什么函数类型?c $ c> = delete
- 是一个特殊的成员函数,
- 除了可能不同的 ref-qualifiers ,除了在复制构造函数或复制赋值运算符的情况下,参数类型可以是引用
非constT
,其中T
是成员函数类的名称)
,就像它已被隐式声明一样, - 没有默认参数。
- 默认构造函数
- 复制构造函数
- 移动构造函数
- 复制赋值运算符
- 移动赋值运算符
- 析构
- 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? - 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
", whereT
is the name of the member function’s class) as if it had been implicitly declared, and - not have default arguments.
- default constructor
- copy constructor
- move constructor
- copy assignment operator
- move assignment operator
- destructor
任何函数都可以声明为已删除( 使用 = delete
)。该标准不施加任何限制。这由C ++ 11 [dcl.fct.def.delete]覆盖。它甚至给出了一个删除的运算符新
的示例。
默认的函数,另一方面, 受限于[dcl.fct.def.default]§1,如下所示:
1 ...是明确默认的
<
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.
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
The "special member functions" referenced in the first bullet point are (as per [special]§1):
这篇关于可以删除非特殊C ++成员函数(或默认)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!