I know =0, =delete
but, what is defaulting a function?
what does somefunc()=default mean?
It means explicitly use the compiler generated default, what the compiler would do if you didn't declare the function. For copy constructor, copy construct each member variable; for the assignment operator, member-wise assignment of each member variable, etc.
Any of the compiler generated member functions, so also the destructor, move constructor and move assignment operator.
So I can only use default for copy, default constructor and assignment operator?
Destructor as well.
EDIT: he beat me!
But why would you explicity declare them default when you can just not declare them at all and save some typing?
Typing has nothing to do with it. You want the code to be as clear as possible. Think of someone (a human) reading your code: If he's going to understand the code better if he is reminded that the compiler will write default implementations for certain functions, it is a good idea to remind him.
Sometimes it's just an elaborate comment. Every now and then you'll have a class where you need to write the default constructor and/or the destructor, but the default generated copy constructor or assignment operator works fine, so you can stick this in so people doing a code review won't automatically flag your class as having a possible rule of three violation. Sometimes you want the default behavior for special member functions that would otherwise be implicitly suppressed. For example, any constructor suppresses generation of the default constructor, and a copy constructor inhibits generation of a move constructor. Both could be re-enabled without explicit implementation with default.
One of the nice things about = default is that you don't need to stick it in the header. For example, this is legal:
This allows you to use the default generated versions, but not have to recompile any code that depends on the header if you change your mind about the implementation.
There are also more language-lawyerish reasons involving POD classes that the committee seems to have spent an inordinate amount of time getting just right. Maybe it will be mystical and magical if I actually need that functionality.
One of the nice things about = default is that you don't need to stick it in the header. For example, this is legal:
// foo.h
struct Foo {
Foo();
Foo(const Foo &);
};
// foo.cpp
Foo::Foo() = default;
Foo::Foo(const Foo &) = default;
This allows you to use the default generated versions, but not have to recompile any code that depends on the header if you change your mind about the implementation.
There are also more language-lawyerish reasons involving POD classes that the committee seems to have spent an inordinate amount of time getting just right. Maybe it will be mystical and magical if I actually need that functionality.
Maybe it will be mystical and magical if I actually need that functionality
This is C++, remember. There are going to be subtle problems with it that will ruin everything else in the language
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement