How does user deleted auto constructors affect implicit generation of copy constructors? For eg:
struct Foo {
Foo(X) = delete;
auto operator=(X) = delete;
};
int main() {
Foo a;
Foo b(a);
b = a;
}
In the above struct, if X is auto
, const auto&
or const auto&&
then the compiler still generate the copy constructor and copy assignment operator, and hence the code is being compiled and executed completely fine.
But if X is auto&
or auto&&
then compiler will not generate the copy constructor and copy assignment operator, and I'm getting 'use of deleted function' error.
I have tried in GCC, Clang and ICC (unfortunately MSVC doesn't support auto parameters still), and same behavior is observed in all three. So, I guess it is somewhere defined in the standard.
Which standard rule guides the aforementioned behavior of compilers?
auto
in a function parameter means exactly the same as replacing it with a template parameter.
So e.g.
Foo(auto&) = delete;
is the same as
template <typename T>
Foo(T&) = delete;
Templated constructors are never copy/move constructors and templated assignment operators are never copy/move assignment operators.
So they do not affect generation of implicit copy/move constructors/assignment oprators at all. No matter whether you use auto
, const auto&
, auto&
, const auto&&
or auto&&
. The implicit copy and move constructor and assignment operators will still be generated.
However, the user-declared templated overloads still participate in overload resolution and may in certain situations be chosen over the implicitly-declared ones in accordance with the the normal overload resolution rules.