As per cppreference doc:
The generation of the implicitly-defined copy constructor is deprecated if T has a user-defined destructor or user-defined copy assignment operator.
But i am not getting any error like default constructor not present.Please correct me if i am wrong.
#include <iostream>
class Demo
{
int val = 100;
public:
~Demo()
{
std::cout << "destructor called\n";
}
Demo& operator= (Demo obj)
{
std::cout << "copy assignment operator called\n";
}
};
int main()
{
Demo obj1;//I am expecting error here : no default constructor present as it is deprecated
Demo obj2(obj1);
return 0;
}
//output:
destructor called
destructor called
>Solution :
The quote talks about the implicitly-defined copy constructor, i.e. the implicit constructor of the form
Demo(const Demo&);
not the implicitly-defined default constructor, i.e. the constructor of the form
Demo();
The latter is what you are using in Demo obj1; and the quote doesn’t apply to it.
The line Demo obj2(obj1); should however generate a deprecation warning, because it does use the implicitly-declared copy constructor, which is deprecated since C++11.
However, the compilers seem to have decided to not warn about this deprecation in general. For example GCC requires -Wextra to emit it. Also, the deprecation has been around for a long time, so it might be questionable whether it will ever actually be removed. Nonetheless, a class declaring a copy assignment and/or destructor explicitly is almost always broken if it doesn’t also define a copy constructor explicitly.