This question already has answers here:
Conditions for automatic generation of default/copy/move ctor and copy/move assignment operator?
(3 answers)
Closed 5 years ago.
This is just a quick question to understand correctly what happens when you create a class with a constructor like this:
class A
{
public:
A() {}
};
I know that no default constructor is generated since it is already defined but are copy and assignment constructors generated by the compiler or in other words do i need to
declare a private copy constructor and a private assignment operator in order to prevent this from happening?
class A
{
private:
// needed to prevent automatic generation?
A( const A& );
A& operator=( const A& );
public:
A() {}
};
Yes, copy constructor and copy assignment operators are still created even if you declare your own default constructor.
The creation of those are only suppressed if you declare your own copy constructor or copy assignment operator in the class definition respectively.
Note that it is possible to have both your own copy constructor, and a compiler provided one:
struct A {
A() { }
A(A const&, int foo);
}; // compiler declares a copy constructor now
// make the second parameter have a default argument
// now this constructor is a copy constructor too.
inline A::A(A const&, int foo = 0) {
}
int main() {
A a;
A b = a; // ambiguity between compiler's one and our custom one!
}
The Standard however allows compilers to accept this code - but the effect is similar to having undefined behavior: The program is ill-formed, but no warning/error is required for that program. (early GCC versions don't reject this code, recent ones reject it).
Yes. The copy constructor, assignment operator, and destructor are always created regardless of other constructors and operators.
If you want to disable one, what you've got there is perfect. It's quite common too.
If you want to disable copying and assigning, then it might be better to inherit from a class that has a private copy constructor and assignment operator (boost::noncopyable is a ready-made one).
1) Less repetitive typing.
2) Self-documenting (hopefully).
3) Stronger checks that those operations can't be invoked (the class itself, nor the friends can make copies either - that would result in a compiler, not a linker error).
4) Won't hide the default constructor :)
#include <boost/noncopyable.hpp>
class X : boost::noncopyable
{
};
int main()
{
X a, b; //has default constructor
//X c(a); //but can't be copied
//a = b; //or assigned
}
Related
I am trying to delete all the copy/move ctor/assignment operators that are implicitly provided, but why am i still able to explicitly delete the default ctor that was supposed to be implicitly deleted?
I have tried =default-ing all the copy/move ctor/assignment operators implicitly provided only to then be told to actually =delete rather than =default. If my understanding of implicit/explicit is correct, the default ctor should be implicitly deleted if the user provides an explicit copy ctor.
I have the following class:
class A {
public:
A() =delete;
A(const A&):...{;} --> my explicitly defined copy ctor
...
}
I expected the compiler to tell me that i cant =delete an implicitly deleted default ctor, but that is not the case. I am using clang8 to complile.
You simply can delete everything you want, fully independent if it was already (implicit) deleted or not.
It is a good idea to show that you did not want to have a default generated function like operators or constructors and "mark" them as deleted. This helps for clarification of the interface!
But: You still can instantiate your class even if you delete the constructor(s)!
class A {
public:
A() =delete;
A(int) = delete;
int a;
void Print() const { std::cout << a << std::endl; }
};
int main()
{
A a{42};
a.Print();
}
See: https://en.cppreference.com/w/cpp/language/aggregate_initialization
From you comment:
I don't want the next programmer to have to look up the inheritance chain to try and figure out what is not already implicitly deleted.
As long as you define a single constructor and do not explicit enable using of base constructors with:
using X::X;
nobody has the need of looking to the base class constructors at all. As said: It is a good idea to mark "unwanted" methods as delete but there are cases, where that did not help in any case as shown above!
I'm having compiler errors when trying to emplace_back in a vector of non-copyable but movable objects with a subtle inheritance twist, that should to my knowledge not change the problem.
Is this legal C++ and this a Visual Studio 2015 bug, or am I making an obvious mistake ?
#include <vector>
class Base
{
public:
Base() {}
Base(Base&) = delete;
Base& operator= (Base&) = delete;
};
class Test : public Base
{
public:
Test(int i) : m_i(i) {}
Test(Test&&) = default;
Test& operator= (Test&&) = default;
protected:
int m_i;
};
int main(int argc, char *argv[])
{
std::vector<Test> vec;
vec.emplace_back(1);
}
Output :
error C2280: 'Test::Test(Test &)': attempting to reference a deleted function
Without the inheritance, that is with deleted copy constructor in Test and no base class, it compiles correctly.
Somehow, removing the default in the move-constructor make it compile correctly also, but then I have to define the move constructor and I don't want to go there.
Which means this compiles fine :
#include <vector>
class Test
{
public:
Test(int i) : m_i(i) {}
Test(Test&) = delete;
Test& operator= (Test&) = delete;
Test(Test&&) = default;
Test& operator= (Test&&) = default;
protected:
int m_i;
};
int main(int argc, char *argv[])
{
std::vector<Test> vec;
vec.emplace_back(1);
}
Puzzling ?
The compiler is correct in all the cases you described.
When Test is derived from Base, its defaulted move constructor is defined as deleted because it tries to move Base, which cannot be move-constructed. Test is actually not move-constructible in your first example.
In your second example, with no base class, there's nothing to prevent the defaulted move constructor from being defined, so Test becomes move-constructible.
When you provide a definition for the move constructor, it's up to you to handle Base. If you just write
Test(Test&&) { }
this will just default-construct a Base object, so the move constructor will compile, but it probably won't do what you want.
Base is not move-constructible because it has a user-declared copy constructor, which prevents the implicit declaration of a move constructor - it has no move constructor at all - and its copy constructor is deleted (it couldn't handle rvalues anyway because it takes a non-const reference).
If you make Base move-constructible, by adding, for example,
Base(Base&&) = default;
then Test also becomes move-constructible, and your example will compile.
One last piece of the puzzle: since we have declared a move constructor for Test, why does the error message reference the deleted copy constructor, even in the first case?
For an explanation of std::vector's logic for choosing which constructor to use to copy / move elements during reallocation, see this answer. Looking at the logic that std::move_if_noexcept uses to choose which kind of reference to return, it will be an rvalue reference in our case (when T is not copy-constructible, the condition is always false). So, we'd still expect the compiler to attempt to call the move constructor.
However, one more rule comes into play: a defaulted move constructor that is defined as deleted does not participate in overload resolution.
This is done so that construction from an rvalue can fall back to a copy constructor taking a const lvalue reference, if available. Note that the rule does not apply when the move constructor is explicitly declared as deleted.
I would like to disable the move constructor in the class. Instead of moving, I would like to base on copy constructor. When I try to write this code:
class Boo
{
public:
Boo(){}
Boo(const Boo& boo) {};
Boo(Boo&& boo) = delete;
};
Boo TakeBoo()
{
Boo b;
return b;
}
during compilation I received error:
error C2280: 'Boo::Boo(Boo &&)': attempting to reference a deleted function
How can I disable the move constructor and force copies instead?
Do not create any move constructor:
class Boo
{
public:
Boo(){}
Boo(const Boo& boo) {};
};
The move constructor is not automatically generated as long as a user-defined copy constructor is present so the copy constructor is called.
Marking a function as =delete makes the function available for overload resolution, but if chosen, the compilation fails; this functionality is not limited to constructors and other special functions (see here). Previously (circa C++03) making the member private achieved a similar result.
Hence, the code as in the sample, effectively means you prohibiting the construction of an object of the class from a temporary or expiring value (rvalues) - the move constructor.
To correct this, remove the move constructor completely. In the case of the class, once a copy constructor is present (user defined), the move is implicitly not generated anyway (move constructor and move assignment operator).
class Boo
{
public:
Boo(){}
Boo(const Boo& boo) {};
//Boo(Boo&& boo) = delete;
};
I don't understand why would I ever do this:
struct S {
int a;
S(int aa) : a(aa) {}
S() = default;
};
Why not just say:
S() {} // instead of S() = default;
why bring in a new syntax for that?
A defaulted default constructor is specifically defined as being the same as a user-defined default constructor with no initialization list and an empty compound statement.
§12.1/6 [class.ctor] A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used to create an object of its class type or when it is explicitly defaulted after its first declaration. The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer (12.6.2) and an empty compound-statement. [...]
However, while both constructors will behave the same, providing an empty implementation does affect some properties of the class. Giving a user-defined constructor, even though it does nothing, makes the type not an aggregate and also not trivial. If you want your class to be an aggregate or a trivial type (or by transitivity, a POD type), then you need to use = default.
§8.5.1/1 [dcl.init.aggr] An aggregate is an array or a class with no user-provided constructors, [and...]
§12.1/5 [class.ctor] A default constructor is trivial if it is not user-provided and [...]
§9/6 [class] A trivial class is a class that has a trivial default constructor and [...]
To demonstrate:
#include <type_traits>
struct X {
X() = default;
};
struct Y {
Y() { };
};
int main() {
static_assert(std::is_trivial<X>::value, "X should be trivial");
static_assert(std::is_pod<X>::value, "X should be POD");
static_assert(!std::is_trivial<Y>::value, "Y should not be trivial");
static_assert(!std::is_pod<Y>::value, "Y should not be POD");
}
Additionally, explicitly defaulting a constructor will make it constexpr if the implicit constructor would have been and will also give it the same exception specification that the implicit constructor would have had. In the case you've given, the implicit constructor would not have been constexpr (because it would leave a data member uninitialized) and it would also have an empty exception specification, so there is no difference. But yes, in the general case you could manually specify constexpr and the exception specification to match the implicit constructor.
Using = default does bring some uniformity, because it can also be used with copy/move constructors and destructors. An empty copy constructor, for example, will not do the same as a defaulted copy constructor (which will perform member-wise copy of its members). Using the = default (or = delete) syntax uniformly for each of these special member functions makes your code easier to read by explicitly stating your intent.
I have an example that will show the difference:
#include <iostream>
using namespace std;
class A
{
public:
int x;
A(){}
};
class B
{
public:
int x;
B()=default;
};
int main()
{
int x = 5;
new(&x)A(); // Call for empty constructor, which does nothing
cout << x << endl;
new(&x)B; // Call for default constructor
cout << x << endl;
new(&x)B(); // Call for default constructor + Value initialization
cout << x << endl;
return 0;
}
Output:
5
5
0
As we can see the call for empty A() constructor does not initialize the members, while B() does it.
n2210 provides some reasons:
The management of defaults has several problems:
Constructor definitions are coupled; declaring any constructor suppresses the default constructor.
The destructor default is inappropriate to polymorphic classes, requiring an explicit definition.
Once a default is suppressed, there is no means to resurrect it.
Default implementations are often more efficient than manually specified implementations.
Non-default implementations are non-trivial, which affects type semantics, e.g. makes a type non-POD.
There is no means to prohibit a special member function or global operator without declaring a (non-trivial) substitute.
type::type() = default;
type::type() { x = 3; }
In some cases, the class body can change without requiring a change in member function definition because the default changes with
declaration of additional members.
See Rule-of-Three becomes Rule-of-Five with C++11?:
Note that move constructor and move assignment operator won't be
generated for a class that explicitly declares any of the other
special member functions, that copy constructor and copy assignment
operator won't be generated for a class that explicitly declares a
move constructor or move assignment operator, and that a class with a
explicitly declared destructor and implicitly defined copy constructor
or implicitly defined copy assignment operator is considered
deprecated
It's a matter of semantics in some cases. It's not very obvious with default constructors, but it becomes obvious with other compiler-generated member functions.
For the default constructor, it would have been possible to make any default constructor with an empty body be considered a candidate for being a trivial constructor, same as using =default. After all, the old empty default constructors were legal C++.
struct S {
int a;
S() {} // legal C++
};
Whether or not the compiler understands this constructor to be trivial is irrelevant in most cases outside of optimizations (manual or compiler ones).
However, this attempt to treat empty function bodies as "default" breaks down entirely for other types of member functions. Consider the copy constructor:
struct S {
int a;
S() {}
S(const S&) {} // legal, but semantically wrong
};
In the above case, the copy constructor written with an empty body is now wrong. It's no longer actually copying anything. This is a very different set of semantics than the default copy constructor semantics. The desired behavior requires you to write some code:
struct S {
int a;
S() {}
S(const S& src) : a(src.a) {} // fixed
};
Even with this simple case, however, it's becoming much more of a burden for the compiler to verify that the copy constructor is identical to the one it would generate itself or for it to see that the copy constructor is trivial (equivalent to a memcpy, basically). The compiler would have to check each member initializer expression and ensure it's identical to the expression to access the source's corresponding member and nothing else, make sure no members are left with non-trivial default construction, etc. It's backwards in a way of the process the compiler would use to verify that it's own generated versions of this function is trivial.
Consider then the copy assignment operator which can get even hairier, especially in the non-trivial case. It's a ton of boiler-plate that you don't want to have to write for many classes but you're be forced to anyway in C++03:
struct T {
std::shared_ptr<int> b;
T(); // the usual definitions
T(const T&);
T& operator=(const T& src) {
if (this != &src) // not actually needed for this simple example
b = src.b; // non-trivial operation
return *this;
};
That is a simple case, but it's already more code than you would ever want to be forced to write for such a simple type as T (especially once we toss move operations into the mix). We can't rely on an empty body meaning "fill in the defaults" because the empty body is already perfectly valid and has a clear meaning. In fact, if the empty body were used to denote "fill in the defaults" then there'd be no way to explicitly make a no-op copy constructor or the like.
It's again a matter of consistency. The empty body means "do nothing" but for things like copy constructors you really don't want "do nothing" but rather "do all the things you'd normally do if not suppressed." Hence =default. It's necessary for overcoming suppressed compiler-generated member functions like copy/move constructors and assignment operators. It's then just "obvious" to make it work for the default constructor as well.
It might have been nice to make default constructor with empty bodies and trivial member/base constructors also be considered trivial just as they would have been with =default if only to make older code more optimal in some cases, but most low-level code relying on trivial default constructors for optimizations also relies on trivial copy constructors. If you're going to have to go and "fix" all your old copy constructors, it's really not much of a stretch to have to fix all your old default constructors, either. It's also much clearer and more obvious using an explicit =default to denote your intentions.
There are a few other things that compiler-generated member functions will do that you'd have to explicitly make changes to support, as well. Supporting constexpr for default constructors is one example. It's just easier mentally to use =default than having to mark up functions with all the other special keywords and such that are implied by =default and that was one of the themes of C++11: make the language easier. It's still got plenty of warts and back-compat compromises but it's clear that it's a big step forward from C++03 when it comes to ease-of-use.
Due to the deprecation of std::is_pod and its alternative std::is_trivial && std::is_standard_layout, the snippet from #JosephMansfield 's answer becomes:
#include <type_traits>
struct X {
X() = default;
};
struct Y {
Y() {}
};
int main() {
static_assert(std::is_trivial_v<X>, "X should be trivial");
static_assert(std::is_standard_layout_v<X>, "X should be standard layout");
static_assert(!std::is_trivial_v<Y>, "Y should not be trivial");
static_assert(std::is_standard_layout_v<Y>, "Y should be standard layout");
}
Note that the Y is still of standard layout.
There is a signicant difference when creating an object via new T(). In case of defaulted constructor aggregate initialization will take place, initializing all member values to default values. This will not happen in case of empty constructor. (won't happen with new T either)
Consider the following class:
struct T {
T() = default;
T(int x, int c) : s(c) {
for (int i = 0; i < s; i++) {
d[i] = x;
}
}
T(const T& o) {
s = o.s;
for (int i = 0; i < s; i++) {
d[i] = o.d[i];
}
}
void push(int x) { d[s++] = x; }
int pop() { return d[--s]; }
private:
int s = 0;
int d[1<<20];
};
new T() will initialize all members to zero, including the 4 MiB array (memset to 0 in case of gcc). This is obviously not desired in this case, defining an empty constructor T() {} would prevent that.
In fact I tripped on such case once, when CLion suggested to replace T() {} with T() = default. It resulted in significant performance drop and hours of debugging/benchmarking.
So I prefer to use an empty constructor after all, unless I really want to be able to use aggregate initialization.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What’s the point in defaulting functions in C++11?
C++11 introduced defaulted methods (e.g. void myMethod() = default;).
What does it do to methods (how do methods behave after being defaulted). How do I use them properly (what are its uses)?
There are a number of class members that are considered "special member functions" by the C++ standard. These are:
The default constructor (a constructor that can be called with no parameters).
The copy constructor (a constructor that can be called with one parameter that is the object type as an lvalue reference).
The copy assignment operator (an operator= overload that can be called with one parameter that is the object type as either an lvalue reference or a value).
The move constructor (a constructor that can be called with one parameter that is the object type as an rvalue reference).
The move assignment operator (an operator= overload that can be called with one parameter that is the object type as either an rvalue reference or a value).
The destructor.
These member functions are special in that the language does special things with them on types. Another thing that makes them special is that the compiler can provide definitions for them if you do not. These are the only functions that you can use the = default; syntax on.
The issue is that the compiler will only provide a definition under certain conditions. That is, there are conditions under which a definition will not be provided.
I won't go over the entire list, but one example is what others have mentioned. If you provide a constructor for a type that is not a special constructor (ie: not one of the constructors mentioned above), a default constructor will not be automatically generated. Therefore, this type:
struct NoDefault
{
NoDefault(float f);
};
NoDefault cannot be default constructed. Therefore, it cannot be used in any context where default construction is needed. You can't do NoDefault() to create a temporary. You can't create arrays of NoDefault (since those are default constructed). You cannot create a std::vector<NoDefault> and call the sizing constructor without providing a value to copy from, or any other operation that requires that the type be DefaultConstructible.
However, you could do this:
struct UserDefault
{
UserDefault() {}
UserDefault(float f);
};
That would fix everything, right?
WRONG!
That is not the same thing as this:
struct StdDefault
{
StdDefault() = default;
StdDefault(float f);
};
Why? Because StdDefault is a trivial type. What does that mean? I won't explain the whole thing, but go here for the details. Making types trivial is often a useful feature to have, when you can do it.
One of the requirements of a trivial type is that it does not have a user-provided default constructor. UserDefault has a provided one, even though it does the exact same thing as the compiler-generated one would. Therefore, UserDefault is not trivial. StdDefault is a trivial type because = default syntax means that the compiler will generate it. So it's not user-provided.
The = default syntax tells the compiler, "generate this function anyway, even if you normally wouldn't." This is important to ensure that a class is a trivial type, since you cannot actually implement special members the way the compiler can. It allows you to force the compiler to generate the function even when it wouldn't.
This is very useful in many circumstances. For example:
class UniqueThing
{
public:
UniqueThing() : m_ptr(new SomeType()) {}
UniqueThing(const UniqueThing &ptr) : m_ptr(new SomeType(*ptr)) {}
UniqueThing &operator =(const UniqueThing &ptr)
{
m_ptr.reset(new SomeType(*ptr)); return *this;
}
private:
std::unique_ptr<SomeType> m_ptr;
};
We must write every one of these functions to make the class copy the contents of the unique_ptr; there's no way to avoid that. But we also want this to be moveable, and the move constructor/assignment operators won't automatically be generated for us. It'd silly to re-implement them (and error-prone if we add more members), so we can use the = default syntax:
class UniqueThing
{
public:
UniqueThing() : m_ptr(new SomeType()) {}
UniqueThing(const UniqueThing &ptr) : m_ptr(new SomeType(*ptr)) {}
UniqueThing(UniqueThing &&ptr) = default;
UniqueThing &operator =(const UniqueThing &ptr)
{
m_ptr.reset(new SomeType(*ptr)); return *this;
}
UniqueThing &operator =(UniqueThing &&ptr) = default;
private:
std::unique_ptr<SomeType> m_ptr;
};
Actually, default can only apply to special methods - i.e. constructors, destructors, assignment operator:
8.4.2 Explicitly-defaulted functions [dcl.fct.def.default]
1 A function definition of the form:
attribute-specifier-seqopt decl-specifier-seqopt declarator = default ;
is called an explicitly-defaulted definition. A function that is explicitly defaulted shall
— 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”, where T is the name of the member function’s class) as if it had been implicitly declared,
and
— not have default arguments.
They behave the same as if the compiler generated them and are useful for cases like:
class Foo{
Foo(int) {}
};
here, the default constructor doesn't exist, so Foo f; wouldn't be valid. However, you can tell the compiler that you want a default constructor without the need to implement it yourself:
class Foo{
Foo() = default;
Foo(int) {}
};
EDIT: Pointed out by #Nicol Bolas in the comments, this truly doesn't explain why you'd prefer this over Foo() {}. What I'm guessing is: if you have a class w/ implementations separated in an implementation file, your class definition (if you don't use =default) would look like:
class Foo{
Foo();
Foo(int);
};
What I'm assuming is that the =default is meant to provide an idiomatic way of telling you "we're not doing anything special in the constructor". With the above class definition, the default constructor could very well not value-initialize the class members. With the =default you have that guarantee just by looking at the header.
Using = default on a constructor or copy-constructor means that the compiler will generate that function for you during compile-time. This happens normally when you exclude the respective functions from your class/struct. And in the case where you define your own constructor, this new syntax allows you to explicitly tell the compiler to default-construct the constructor where it normally won't. Take for instance:
struct S {
};
int main() {
S s1; // 1
S s2(s1); // 2
}
The compiler will generate both a copy-constructor and a default-constructor, thus allowing us to instantiate an S object like we did (1) and copy s2 into s1 (2). But if we define our own constructor, we find that we are unable to instantiate in the same way:
struct S {
S(int);
};
int main() {
S s; // illegal
S s(5); // legal
}
This fails because S has been given a custom-defined constructor, and the compiler won't generate the default one. But if we still want the compiler to give us one, we can explicitly convey this using default:
struct S {
S(int);
S() = default;
S(const S &) = default;
};
int main() {
S s; // legal
S s(5); // legal
S s2(s); // legal
}
Note, however, that only certain functions with certain function-signatures can be overloaded. It follows that this will fail because it is neither a copy-constructor nor a default-constructor or a move or assignment operator:
struct S {
S(int) = default; // error
};
We are also able to explicitly define the assignment operator (just like the default/copy-constructor, a default one is generated for us when we do not write out our own). So take for example:
struct S {
int operator=(int) {
// do our own stuff
}
};
int main() {
S s;
s = 4;
}
This compiles, but it won't work for us in the general case where we want to assign another S object into s. This is because since we wrote our own, the compiler doesn't provide one for us. This is where default comes into play:
struct S {
int operator=(int) {
// do our own stuff
}
S & operator=(const S &) = default;
};
int main() {
S s1, s2;
s1 = s2; // legal
}
But technically it's superfluous to define the assignment operator, copy, or move constructor as default when there is no other function "overriding" it as the compiler will provide one for you in that case anyway. The only case in which it won't provide you one is where you define your own one (or variation thereof). But as a matter of preference, asking the compiler to provide a default function using the new aforementioned syntax is more explicit and easier to see and understand if intentionally left out for the compiler.