为什么不能显式默认带有 volatile 参数的复制构造函数?

Why is it impossible to explicitly default a copy constructor with volatile argument?

我找不到标准中的哪里规定禁止使用 volatile&const volatile& 参数显式默认复制构造函数和复制赋值,如下所示:

struct A{
   A(const volatile A&) =default; // fails to compile on (all) compilers
   };

在[dcl.fct.def.default]中没有这样的限制,而[class.copy]指定A(const volatile A&)是一个复制构造函数。

注意:我只是在标准文本中查找指定此行为的位置。

您在正确的部分,但忽略了一些关键要点。

[dcl.fct.def.default]/1:

A function definition of the form:

...

is called an explicitly-defaulted definition. A function that is explicitly defaulted shall

  • 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

[class.copy.ctor]/7:

The implicitly-declared copy constructor for a class X will have the form

X::X(const X&)

if each potentially constructed subobject of a class type M (or array thereof) has a copy constructor whose first parameter is of type const M& or const volatile M&.119 Otherwise, the implicitly-declared copy constructor will have the form

X::X(X&)

...
119) This implies that the reference parameter of the implicitly-declared copy constructor cannot bind to a volatile lvalue;

总结以上内容后,明确默认复制 c'tor 的唯一两个选项是:

struct A {
   A(const A&) = default;
};

struct B {
   B(B&) = default;
};

当标准说 A(const volatile A&) 是复制构造函数时。这意味着具有这样一个参数的 user-provided c'tor 可以是 类 copy c'tor。