Jump to content

  • Log In with Google      Sign In   
  • Create Account


what does somefunc()=default mean?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 21 October 2012 - 05:03 PM

I know =0, =delete
but, what is defaulting a function?
An invisible text.

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9340

Like
5Likes
Like

Posted 21 October 2012 - 05:07 PM

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.

#3 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 21 October 2012 - 05:10 PM

So I can only use default for copy, default constructor and assignment operator?
An invisible text.

#4 SiCrane   Moderators   -  Reputation: 9340

Like
5Likes
Like

Posted 21 October 2012 - 05:45 PM

Any of the compiler generated member functions, so also the destructor, move constructor and move assignment operator.

#5 joew   Crossbones+   -  Reputation: 3471

Like
0Likes
Like

Posted 21 October 2012 - 05:46 PM

So I can only use default for copy, default constructor and assignment operator?

Destructor as well.

EDIT: he beat me!

Edited by Saruman, 21 October 2012 - 05:47 PM.


#6 lride   Members   -  Reputation: 633

Like
0Likes
Like

Posted 21 October 2012 - 06:03 PM

But why would you explicity declare them default when you can just not declare them at all and save some typing?
An invisible text.

#7 Álvaro   Crossbones+   -  Reputation: 11723

Like
1Likes
Like

Posted 21 October 2012 - 06:42 PM

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.

#8 SiCrane   Moderators   -  Reputation: 9340

Like
11Likes
Like

Posted 21 October 2012 - 07:08 PM

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:
// 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.

#9 InvalidPointer   Members   -  Reputation: 1356

Like
0Likes
Like

Posted 25 October 2012 - 09:08 PM

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 :)
clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS