Topic: Default constructor


Author: Chubs Dad <chubsdad@gmail.com>
Date: Sun, 19 Sep 2010 17:00:33 CST
Raw View
> What would be the advantage of this? Destructors of base-classes
> and members are called implicitly anyway. What should be the
> reason to enforce a derived class to implement the destructor,
> if there is nothing to do? Just nannying programmers? [This question
> was meant seriously, even though I added some humor into my
> question]. Seriously, I have never missed such a feature (yet).
> You cannot even enforce this for a derived class, if the base class
> has a pure virtual destructor.

I have been reading about the Big Three and hence thought that
shouldn't the same kind of rules be applied to all the three. So if
the implicit default constructor is suppressed, so should the
destructor be.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: Chubs Dad <chubsdad@gmail.com>
Date: Wed, 15 Sep 2010 12:09:35 CST
Raw View
struct Base{
   Base(Base &){}        // suppress default constructor
};

int main(){
   Base b;
}

The code shown gives error because the default constructor (implicit)
of 'Base' is suppressed. Indeed the standard says in $12.1 "If there
is no user-declared constructor for class X, a default constructor is
implicitly declared."

There are three things that I am unable to understand.

a) Does the standard say anywhere that if the user declared
constructor is present in a class, the default constructor (implicit)
is suppressed. It is bascically the above phrased negatively or is it
once again implied :)?

b) Why is it that way? Why does an explicitly declared copy
constructor suppress the implicit default constructor?

c) Why the same rules do not apply for the default destructor?

I was looking at this, from the point of view of suppressing the
implicitly defined destructor of a class thereby forcing derived
classes (from Base) to provide an explicit destructor definition.

Regards,
Chubsdad

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Bo Persson" <bop@gmb.dk>
Date: Wed, 15 Sep 2010 16:13:08 CST
Raw View
Chubs Dad wrote:
> struct Base{
>   Base(Base &){}        // suppress default constructor
> };
>
> int main(){
>   Base b;
> }
>
> The code shown gives error because the default constructor
> (implicit) of 'Base' is suppressed. Indeed the standard says in
> $12.1 "If there is no user-declared constructor for class X, a
> default constructor is implicitly declared."
>
> There are three things that I am unable to understand.
>
> a) Does the standard say anywhere that if the user declared
> constructor is present in a class, the default constructor
> (implicit) is suppressed. It is bascically the above phrased
> negatively or is it once again implied :)?

The fact that one case is explicitly specified implies that the
opposite does not apply. Why would 12.1 say this if the default
constructor was always provided?

>
> b) Why is it that way? Why does an explicitly declared copy
> constructor suppress the implicit default constructor?

It doesn't really. The rules are that if you don't provide any
constructors, like in a C style struct, some will be provided for you
(so that the C struct can be constructed, copied, and assigned).

If you actually provide one or more constructors, it is assumed that
those are the ones you wanted.  :-)

>
> c) Why the same rules do not apply for the default destructor?

It does. If you write a destructor, the defaukt one isn't generated.
.-)

>
> I was looking at this, from the point of view of suppressing the
> implicitly defined destructor of a class thereby forcing derived
> classes (from Base) to provide an explicit destructor definition.
>

The usual way to do that is to make the destructor pure virtual.

virtual ~Base() = 0;



Bo Persson



--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: David Krauss <potswa@gmail.com>
Date: Wed, 15 Sep 2010 16:11:56 CST
Raw View
On Sep 15, 1:09=A0pm, Chubs Dad <chubs...@gmail.com> wrote:
> Indeed the standard says in $12.1 "If there
> is no user-declared constructor for class X, a default constructor is
> implicitly declared."
>
> There are three things that I am unable to understand.
>
> a) Does the standard say anywhere that if the user declared
> constructor is present in a class, the default constructor (implicit)
> is suppressed. It is bascically the above phrased negatively or is it
> once again implied :)?

Yep. If the standard doesn't explicitly require the implicit
declaration, there is no implicit implicit declaration to fall back
on ;v) . Be thankful the language is as simple and non-redundant as it
is!

> b) Why is it that way? Why does an explicitly declared copy
> constructor suppress the implicit default constructor?

The Standard regards the implicitly defined constructors as "trivial."
If you define one constructor with non-trivial semantics, having other
implicit constructors failing to satisfy those semantics would be bad.
This is so important that defining even the copy constructor, which is
"special," invokes exclusively user-defined semantics.

> c) Why the same rules do not apply for the default destructor?

The existence of an implicit destructor doesn't create the possibility
of accidental construction. The implicit destructor is still useful,
for example, if the constructor mainly serves to accept arguments and
assign them to members. Which is quite common.

> I was looking at this, from the point of view of suppressing the
> implicitly defined destructor of a class thereby forcing derived
> classes (from Base) to provide an explicit destructor definition.

You even provided an answer in the StackOverflow thread where Johannes
solved this problem, two days ago:

http://stackoverflow.com/questions/3700468

I can't imagine what real-world problem this actually solves, though.
The implicit destructor is the same as the empty destructor, which is
all you can ask the user to define.

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "\"Alf P. Steinbach /Usenet\" <alf.p.steinbach+usenet@gmail.com"<alf.p.steinbach%2Busenet@gmail.com>
Date: Wed, 15 Sep 2010 17:16:31 CST
Raw View
* Chubs Dad, on 15.09.2010 20:09:

> struct Base{
>    Base(Base&){}        // suppress default constructor
> };
>
> int main(){
>    Base b;
> }
>
> The code shown gives error because the default constructor (implicit)
> of 'Base' is suppressed. Indeed the standard says in $12.1 "If there
> is no user-declared constructor for class X, a default constructor is
> implicitly declared."
>
> There are three things that I am unable to understand.
>
> a) Does the standard say anywhere that if the user declared
> constructor is present in a class, the default constructor (implicit)
> is suppressed. It is bascically the above phrased negatively or is it
> once again implied :)?
>

Assuming your quote is correct, that's it.

It implies that if there is a used-declared constructor, then implicit
declaration of a default constructor will not happen.


b) Why is it that way? Why does an explicitly declared copy
> constructor suppress the implicit default constructor?
>

Because any user declared constructor says that the user is taking charge of
construction: objects should only be instantiable via the user constructors.



c) Why the same rules do not apply for the default destructor?
>

I think you mean why not same rules for copy constructor. Well the copy
constructor is special: you can't use it to obtain an X object without
already having an X object, and a compiler is allowed to assume (for the
purpose of eliding calls) that a copy constructor copy constructs, and
nothing else. So for the copy constructor there is both a generally
reasonable default, and no interaction with the general instantiation of X
objects that the user is taking charge of by declaring a constructor.

I was looking at this, from the point of view of suppressing the
> implicitly defined destructor of a class thereby forcing derived
> classes (from Base) to provide an explicit destructor definition.
>

Uhm, why?


Cheers & hth.,

- Alf

--
blog at <url: http://alfps.wordpress.com>

[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Wed, 15 Sep 2010 17:16:48 CST
Raw View
On 15 Sep., 20:09, Chubs Dad <chubs...@gmail.com> wrote:
> struct Base{
>    Base(Base &){}        // suppress default constructor
> };
>
> int main(){
>    Base b;
> }
>
> The code shown gives error because the default constructor (implicit)
> of 'Base' is suppressed. Indeed the standard says in $12.1 "If there
> is no user-declared constructor for class X, a default constructor is
> implicitly declared."
>
> There are three things that I am unable to understand.
>
> a) Does the standard say anywhere that if the user declared
> constructor is present in a class, the default constructor (implicit)
> is suppressed. It is bascically the above phrased negatively or is it
> once again implied :)?

The above wordings says all that is needed. This is the way
how special member functions are specified: There exists no
suppression, but only potential implicit declaration. This is
related to the fact that implicitly declared members are very
special anyway. The copy-constructor is also implicitly
declared, unless some conditions prevent that. In C++03 the
circumstances are very broad: The copy-constructor is implicitly
declared *unless* there exists a user-provided declaration for a
copy-constructor: This means that while in

struct S {};

there is an implicitly declared copy-constructor of the signature

S(const S);

but in the type

struct P { (P&); };

there is *no* copy-constructor declared, not even the otherwise
valid form:

P(const P&);

> b) Why is it that way? Why does an explicitly declared copy
> constructor suppress the implicit default constructor?

Some form of heuristic: To make it easier for programmers
to "suppress" the implicit default constructor declaration,
because the class might not be useful with one. Many
classes without default-constructor do still have a copy
constructor.

> c) Why the same rules do not apply for the default destructor?

See above. In C++0x you can "reintroduce" the compiler-
declared form by defaulting the default-constructor.

> I was looking at this, from the point of view of suppressing the
> implicitly defined destructor of a class thereby forcing derived
> classes (from Base) to provide an explicit destructor definition.

What would be the advantage of this? Destructors of base-classes
and members are called implicitly anyway. What should be the
reason to enforce a derived class to implement the destructor,
if there is nothing to do? Just nannying programmers? [This question
was meant seriously, even though I added some humor into my
question]. Seriously, I have never missed such a feature (yet).
You cannot even enforce this for a derived class, if the base class
has a pure virtual destructor.

HTH & Greetings from Bremen,

Daniel Kr=FCgler

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "Johannes Schaub (litb)" <schaub-johannes@web.de>
Date: Wed, 15 Sep 2010 17:26:46 CST
Raw View
Chubs Dad wrote:

> struct Base{
>    Base(Base &){}        // suppress default constructor
> };
>
> int main(){
>    Base b;
> }
>
> The code shown gives error because the default constructor (implicit)
> of 'Base' is suppressed. Indeed the standard says in $12.1 "If there
> is no user-declared constructor for class X, a default constructor is
> implicitly declared."
>
> There are three things that I am unable to understand.
>
> a) Does the standard say anywhere that if the user declared
> constructor is present in a class, the default constructor (implicit)
> is suppressed. It is bascically the above phrased negatively or is it
> once again implied :)?
>

Things don't happen out of nowhere. That's why the Standard doesn't need to
state that your class shall not have a "zombie"-named function declared
implicitly. Likewise it doesn't need to state that there is no default
constructor declared implicitly.

> b) Why is it that way? Why does an explicitly declared copy
> constructor suppress the implicit default constructor?
>

Copy constructors are also constructors and the rule applies generically
here. For the following, I can't imagine what senseful action the default
constructor should do

struct MutexLocker {
 MutexLocker(Mutex&);
};

But I think the Standard could special handle copy constructors once again
and say the rule applies except when the declared constructor is a copy
constructor. But is this really a good thing? Mostly, if you need a copy
constructor, something has created some resource that you need to copy over
by it -> that's where the Standard applies some visionary powers and
grumbles "Oh he will write another constructor anyway."

Finally note that it's possible for a constructor to be both a default
constructor and a copy constructor, from the class definition onwards

struct A {
 static A a;
 A(A const& = a);
};

> c) Why the same rules do not apply for the default destructor?
>

There is only one destructor and it does need to do a very important thing:
Calling destructors of bases and members. It does not seem to make sense to
me to make it another way.

> I was looking at this, from the point of view of suppressing the
> implicitly defined destructor of a class thereby forcing derived
> classes (from Base) to provide an explicit destructor definition.
>

I see that you have also thought about that "force explicit destructor"
question like I did :) But really, even if you write an explicit destructor,
you need to say in some way "Don't call the base destructor because it
doesn't exist - I suppessed it!". Or did I misunderstand something?

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]