I am trying to run this example in https://en.cppreference.com/w/cpp/language/copy_assignment, but when I delete the default constructor and default copy constructor: A() = default; A(A const&) = default;
, clang++ says that warning: definition of implicit copy constructor for 'A' is deprecated because it has a user-provided copy assignment operator [-Wdeprecated-copy-with-user-provided-copy]
.
My question is that I have called copy assignment instead of copy constructor, why clang++ reports copy constructor warning?
Here is my code:
#include <iostream>
#include <memory>
#include <string>
#include <algorithm>
struct A
{
int n;
std::string s1;
// user-defined copy assignment (copy-and-swap idiom)
A& operator=(A other)
{
std::cout << "copy assignment of A\n";
std::swap(n, other.n);
std::swap(s1, other.s1);
return *this;
}
};
int main()
{
A a1, a2;
std::cout << "a1 = a2 calls ";
a1 = a2; // user-defined copy assignment
}
Here is cppinsight link, I can see there is an inline copy constructor in struct A.
struct A
{
int n;
std::basic_string<char> s1;
inline A & operator=(A other)
{
std::operator<<(std::cout, "copy assignment of A\n");
std::swap(this->n, other.n);
std::swap(this->s1, other.s1);
return *this;
}
// inline A(const A &) noexcept(false) = default;
// inline ~A() noexcept = default;
// inline A() noexcept = default;
};