Thread View: comp.std.c++
3 messages
3 total messages
Started by Pulkkinen Esa
Tue, 05 Sep 1995 15:05
Explicit-keyword and default constructors
Author: Pulkkinen Esa
Date: Tue, 05 Sep 1995 15:05
Date: Tue, 05 Sep 1995 15:05
46 lines
2260 bytes
2260 bytes
I'm sorry, if this question has been asked before, I remember a related
discussion, but I can't recall, if this question was answered.
Consider this example:
class A
{
public:
explicit A() {}
};
A a; // Legal? (IMHO: no)
A b=A(); // OK, as always
A c(); // OK, c is a function returning A :)
class B : public A {}
// Default constructor generated, and calls A()? Legal?
// (IMHO: no)
What is the current interpretation of the above program?
My analysis:
[dcl.fct.spec]/5 states:
"The explicit-specifier shall be used only in declarations of constructors
within a class declaration."
[class.conv.ctor]/1:
"A constructor declared without the function-specifier explicit
that can be called with a single parameter specifies a conversion
from the type of its first parameter to the type of its class.
Such a constructor is called a converting constructor."
These were the only places I could find, where the semantics of
the explicit specifier was explained. The first one clearly (?) states
that the explicit-specifier can be used for any kind of constructor.
The [class.conv.ctor] only defines the behaviour when the constructor
can be called with one parameter.
So, where does the WP define the semantics of an explicit-keyword
applied to a default constructor (or any other constructor that can't
be called with a single parameter...)?
---
Esa Pulkkinen | C++ programmers do it virtually always
esap@cs.tut.fi | with classes, resulting in multiple
http://www.cs.tut.fi/~esap/ | inheritance.
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Explicit-keyword and default constructors
Author: Steve Clamage
Date: Tue, 05 Sep 1995 16:14
Date: Tue, 05 Sep 1995 16:14
65 lines
2404 bytes
2404 bytes
In article 1m9@proffa.cc.tut.fi, p150651@proffa.cc.tut.fi (Pulkkinen Esa) writes:
>
>Consider this example:
>
>class A
>{
>public:
> explicit A() {}
>};
>A a; // Legal? (IMHO: no)
Legal.
>A b=A(); // OK, as always
Yes.
>A c(); // OK, c is a function returning A :)
Yes.
>
>class B : public A {}
>// Default constructor generated, and calls A()? Legal?
>// (IMHO: no)
Legal.
I don't think placing "explicit" on other than a one-argument constructor
has any effect. By "one-argument constructor" I mean one which may be
invoked with one argument; it might have additional arguments with default
values. The only discussion of the "explicit" keyword that I can find
in the draft standard concerns only one-argument constructors.
A one-argument constructor is a type conversion from the argument type
to the class type. Such a public constructor may be used implicilty by
the compiler whenever that conversion is required.
The "explicit" keyword prevents using a one-argument constructor for
implicit type conversion. All explicit uses of the constructor are still OK.
Example:
class A {
public:
explicit A(int);
};
foo(const A&);
...
foo(3); // error
The call to foo would be OK if A were not marked "explicit". The compiler
would generate an implicit conversion of an "int" to an "A". But the
marked constructor cannot be used for that purpose. Similarly:
A a(3); // OK, explicit
a = A(4); // OK, explicit conversion, then assignment
a = 5; // error, can't convert "int" to "A" implicitly
---
Steve Clamage, stephen.clamage@eng.sun.com
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Re: Explicit-keyword and default constructors
Author: Nathan Myers
Date: Tue, 05 Sep 1995 21:06
Date: Tue, 05 Sep 1995 21:06
37 lines
1620 bytes
1620 bytes
In article <42gjou$1m9@proffa.cc.tut.fi>,
Pulkkinen Esa <p150651@proffa.cc.tut.fi> wrote:
>class A
>{
>public:
> explicit A() {}
>};
>A a; // Legal? (IMHO: no) [yes]
>A b=A(); // OK, as always
>A c(); // OK, c is a function returning A :)
>
>class B : public A {}
>// Default constructor generated, and calls A()? Legal?
>// (IMHO: no) ... [yes]
>My analysis:
>[dcl.fct.spec]/5 states: ...
>[class.conv.ctor]/1: ...
>
>These were the only places I could find, where the semantics of
>the explicit specifier was explained.
Esa has called attention to an embarrassment: the April '95 Draft
doesn't actually specify *any* semantics for "explicit", because
it defines "nonconverting constructor" but never uses the term.
Steve's explanation of the intent is accurate, though. The omission
is considered "editorial", which means fixing it doesn't require a
committee vote.
Nathan Myers
myersn@roguewave.com
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads