Topic: explicit" default constructor?
Author: Stein Somers <stein.somers@intec.rug.ac.be>
Date: 1997/09/04 Raw View
(not about converting constructors)
Long ago, on the same subject, Steve Clamage wrote
> If a constructor has no parameters,
> declaring it "explicit" has no effect,
> since it can never be invoked for a conversion.
[class.conv.ctor] was reshaped since then but CD2 still mentions
explicit default constructors in 12.3.1.2:
"A default constructor may be an explicit constructor; such
a constructor will be used to perform default=ADinitialization (8.5).
[Example:
class Z {
public:
explicit Z();
explicit Z(int);
// ...
};
Z a; // ok: default=ADinitialization performed
"
Uh? If there is a default constructor, isn't it *always* used
to perform default=ADinitialization? Looking closer (and looking more
and more confused), 8.5.5 suggests a default=ADinitialized POD-object
should be zero-initialised. Does this mean then that given:
struct POD
{
int i;
POD() : i(1) {}
}
=09
a POD variable should have its i initialised as 0, where it would be 1
if the default construct were tagged explicit? Not so, says Borland.
Stein
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Steve Clamage <stephen.clamage_nospam@Eng.Sun.COM>
Date: 1997/09/08 Raw View
Stein Somers wrote:
>
> [class.conv.ctor] was reshaped since then but CD2 still mentions
> explicit default constructors in 12.3.1.2:
>
> "A default constructor may be an explicit constructor; such
> a constructor will be used to perform default initialization (8.5).
> [Example: ... "
>
>
> Uh? If there is a default constructor, isn't it *always* used
> to perform default initialization? Looking closer (and looking more
> and more confused), 8.5.5 suggests a default initialized POD-object
> should be zero-initialised. Does this mean then that given:
>
> struct POD
> {
> int i;
> POD() : i(1) {}
> }
>
> a POD variable should have its i initialised as 0, where it would be 1
> if the default construct were tagged explicit?
Among the limitations on POD-structs is that they have no
user-defined constructor. See 8.5.1 "Aggregates" and 9 "Classes".
If your struct were really a POD, it would be zero-initialized.
But since it isn't a POD, the default constructor is used.
--
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
Author: Stein Somers <stein.somers@intec.rug.ac.be>
Date: 1997/09/09 Raw View
Steve Clamage wrote:
> Among the limitations on POD-structs is that they have no
> user-defined constructor.
Oops, I thought so but completely overlooked aggregate.
What a silly idea to stick my neck out in c.s.c++.
Still CD2 [class.conv.ctor] maintains mysteriously:
"A default constructor may be an explicit constructor; such
a constructor will be used to perform default=ADinitialization."
A constructor is declared, so we're looking at a non-POD, thus the
constructor will be used to perform default=ADinitialization anyway.
Superfluous sentence in the draft then?
Besides, CD2 doesn't explicitely define 'explicit constructor'.
It's the complement of 'converting constructor', I guess.
Summary:
* "The explicit specifier shall be used only in declarations
of constructors within a class declaration" [7.1.2 6]
* explicit matters only to constructors "that can be called with
a single parameter" [12.3.1 1]
* and it bears no magic solution to unrelated problems
Stein
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/03/27 Raw View
In article F1@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
writes:
>as I read the section [class.conv.ctor] of the DWP, I get the impression that
>the description of converting and non-converting constructors applies to all
>constructors, i.e. a constructor is either converting or non-converting. A
>non-converting constructor creates an object "only where a constructor call
>is explicitly indicated by the syntax". This has no implications on
>constructors with more than one parameter, but what about the default
>constructor? Should a default constructor call be really indicated by the
>syntax and if so, how is it to be done? What if the default constructor is
>declared "explicit"?
A converting constructor is one which can be called with a single parameter.
The default constructor isn't a converting constructor except in the
case of a constructor with all parameters having default values.
If a constructor has no parameters, declaring it "explicit" has no effect,
since it can never be invoked for a conversion.
If a constructor's parameters all have default values, it can be a converting
constructor, and declaring it "explicit" prevents it from being invoked
implicitly.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
Date: 1996/03/28 Raw View
Steve Clamage wrote:
> In article F1@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
> writes:
>
> > as I read the section [class.conv.ctor] of the DWP, I get the
> > impression that the description of converting and non-converting
> > constructors applies to all constructors, i.e. a constructor is either
> > converting or non-converting. A non-converting constructor creates an
> > object "only where a constructor call is explicitly indicated by the
> > syntax". This has no implications on constructors with more than one
> > parameter, but what about the default constructor? Should a default
> > constructor call be really indicated by the syntax and if so, how is it
> > to be done? What if the default constructor is declared "explicit"?
>
> A converting constructor is one which can be called with a single parameter.
> The default constructor isn't a converting constructor except in the
> case of a constructor with all parameters having default values.
>
> If a constructor has no parameters, declaring it "explicit" has no effect,
> since it can never be invoked for a conversion.
>
Exactly this is my question. Does this section deal only with constructors
with one parameter ( i.e. those that could be converting )? I read it this
way: if a constructor is not a converting constructor, i.e. it either doesn't
have exactly one parameter or is declared explicit, it is a non-converting
constructor and a call should be explicitly indicated by the syntax. In this
case the default constructor is non-converting.
> If a constructor's parameters all have default values, it can be a
> converting constructor, and declaring it "explicit" prevents it from
> being invoked implicitly.
Yes, I've had this in mind, too. In the following case:
class A
{
explicit A( int x=0 );
};
how do you call the default constructor? Can it still be invoked implicitly
and what is the "explicit" syntax if not? IMO, something like
A a;
is not explicit ( maybe I'm wrong here and this does explicitly call the
default constructor explicitly - but where does the DWP define it? ). An
explicit call would rather be
A a();
but what is the point in using this syntax?
Bye
Roman
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: clamage@Eng.sun.com (Steve Clamage)
Date: 1996/03/28 Raw View
In article 6AF5@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:
>Steve Clamage wrote:
>
>> A converting constructor is one which can be called with a single parameter.
>> The default constructor isn't a converting constructor except in the
>> case of a constructor with all parameters having default values.
>>
>> If a constructor has no parameters, declaring it "explicit" has no effect,
>> since it can never be invoked for a conversion.
>
>> If a constructor's parameters all have default values, it can be a
>> converting constructor, and declaring it "explicit" prevents it from
>> being invoked implicitly.
>
>Yes, I've had this in mind, too. In the following case:
>
>class A
>{
> explicit A( int x=0 );
>};
>
>how do you call the default constructor?
The same way you always did before. The default constructor is one which
requires no parameters. If implicitly or explicitly invoked with no
parameters, no conversion is taking place, and it is a non-converting
constructor.
> Can it still be invoked implicitly
>and what is the "explicit" syntax if not? IMO, something like
>
>A a;
>
>is not explicit
I don't think it matters whether you consider this explicit or not.
(I would say it is implicit.) No type conversion is taking place, so
whether the constructor is declared "explicit" or not doesn't matter.
> An explicit call would rather be
>
>A a();
No, that declares 'a' to be a function with no parameters returning an
'A' by value. It is an unfortunate consequence of C declaration syntax
colliding with C++ intialization syntax.
You can invoke the default constructor explicitly to create an anonymous
temporary object in the same way as before, whether or not you supply
parameters, and whether or not it is declared explicit. Examples:
class C {
public:
C(int=0);
};
foo( const C& );
foo( C() ); // #1
foo( C(2) ); // #2
foo( 3 ); // #3
In #1, C's (default) constructor is explicitly invoked to create a temp
object to be passed to foo. It doesn't matter if the constructor was
declared explicit or not in this case.
In #2, C's constructor is explicitly invoked to convert the int value 2 to
a temp C object to be passed to foo. Again, C::C() can be explicit or not.
In #3, C's constructor is invoked implicitly to convert the int value 3 to
a temp C object to be passed to foo. But if the constructor were declared
"explicit", this use would be invalid. You would have to use the form of #2.
---
Steve Clamage, stephen.clamage@eng.sun.com
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: vandevod@cs.rpi.edu (David Vandevoorde)
Date: 1996/03/28 Raw View
>>>>> "RL" == Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:
[...]
>> If a constructor's parameters all have default values, it can be a
>> converting constructor, and declaring it "explicit" prevents it from
>> being invoked implicitly.
RL> Yes, I've had this in mind, too. In the following case:
RL> class A
RL> {
RL> explicit A( int x=0 );
RL> };
RL> how do you call the default constructor? Can it still be invoked
RL> implicitly and what is the "explicit" syntax if not? IMO, something like
RL> A a;
RL> is not explicit ( maybe I'm wrong here and this does explicitly call the
RL> default constructor explicitly - but where does the DWP define it? ). An
RL> explicit call would rather be
Yes: the above is quite ``explicit''. Implicit would be:
a = 23; // Implicitly equivalent to `a = A(23)' if no `explicit'
// specification (and no preferred int-assignment
// alternative).
RL> A a();
This is not a constructor invacation anyway (it's the declaration of
a function `a' returning an object of type `A').
Daveed
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
Date: 1996/03/28 Raw View
Steve Clamage wrote:
>
> In article 6AF5@cs.tu-berlin.de, Roman Lechtchinsky <wolfro@cs.tu-berlin.de> writes:
> >Steve Clamage wrote:
> >
> >> A converting constructor is one which can be called with a single parameter.
> >> The default constructor isn't a converting constructor except in the
> >> case of a constructor with all parameters having default values.
> >>
> >> If a constructor has no parameters, declaring it "explicit" has no effect,
> >> since it can never be invoked for a conversion.
> >
> >> If a constructor's parameters all have default values, it can be a
> >> converting constructor, and declaring it "explicit" prevents it from
> >> being invoked implicitly.
> >
> >Yes, I've had this in mind, too. In the following case:
> >
> >class A
> >{
> > explicit A( int x=0 );
> >};
> >
> >how do you call the default constructor?
>
> The same way you always did before. The default constructor is one which
> requires no parameters. If implicitly or explicitly invoked with no
> parameters, no conversion is taking place, and it is a non-converting
> constructor.
Sorry for posting another article on this topic ( I wasn't going to, honest )
but I'm really not happy with this section of the DWP ( you might remember
the thread about copy constructors ). However, you agree that a default
constructor is a non-converting constructor. Then I read this passage in the
DWP:
A nonconverting constructor constructs objects just like converting
constructors, but does so only where a constructor call is explicitly
indicated by the syntax. [class.conv.ctor]
Do you see how this can be interpreted? A default constructor is
non-converting, so it constructs objects only if its call is "explicitly
indicated". The DWP doesn't say that this section deals only with
constructors that could be converting or are invoked for a conversion. BTW,
I'm talking only about the definitions in the DWP, not about the language
itself (i.e. I'm not confused how to call default constructors). So, if we
argue strictly on the basis of the DWP, where am I wrong?
>
> > An explicit call would rather be
> >
> >A a();
>
> No, that declares 'a' to be a function with no parameters returning an
> 'A' by value.
Oops. Sure. Sorry. Probably I should get some sleep.
Bye
Roman
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Roman Lechtchinsky <wolfro@cs.tu-berlin.de>
Date: 1996/03/27 Raw View
Hi,
as I read the section [class.conv.ctor] of the DWP, I get the impression that
the description of converting and non-converting constructors applies to all
constructors, i.e. a constructor is either converting or non-converting. A
non-converting constructor creates an object "only where a constructor call
is explicitly indicated by the syntax". This has no implications on
constructors with more than one parameter, but what about the default
constructor? Should a default constructor call be really indicated by the
syntax and if so, how is it to be done? What if the default constructor is
declared "explicit"?
Bye
Roman
---
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]