C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Multiple default constructors

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Alf P. Steinbach
Guest





PostPosted: Tue Mar 22, 2005 8:40 pm    Post subject: Multiple default constructors Reply with quote



Is there any reason why the standard doesn't forbid multiple default
constructors for a class?

Or does it, and I'm unaware of it?

Or is there some way to call such a constructor without specifying
any arguments?


PS: If anybody knows, why is "..." permitted for a default constructor
but not for a copy constructor, where optional args must be defaulted?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Back to top
Hyman Rosen
Guest





PostPosted: Tue Mar 22, 2005 9:26 pm    Post subject: Re: Multiple default constructors Reply with quote



Alf P. Steinbach wrote:
Quote:
Is there any reason why the standard doesn't forbid multiple default
constructors for a class?

Why should it? It's just handled by the normal overload ambiguity rules.

Quote:
Or is there some way to call such a constructor without specifying
any arguments?

Nope.

Quote:
PS: If anybody knows, why is "..." permitted for a default constructor
but not for a copy constructor, where optional args must be defaulted?

What do you mean? Why do you think ... is forbidden for a copy constructor?
What does ... have to do with defaulting optional parameters?

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Tue Mar 22, 2005 11:05 pm    Post subject: Re: Multiple default constructors Reply with quote



* Hyman Rosen:
Quote:
* Alf P. Steinbach:

Is there any reason why the standard doesn't forbid multiple default
constructors for a class?

Why should it? It's just handled by the normal overload ambiguity rules.

Because (1) as opposed to an ordinary member function T() cannot be called
when overloads like T(int=0) exist, it's dead code -- at least AFAIK, and
(2) the point of providing defaults is to let the client code avoid specifying
that which is defaulted, but for a default constructor there's no way.

Thus it seems that the case of multiple default constructors, especially with
one T(), can never be intentional (if the assumption of no possibility of call
sans explicit arguments is correct, as we seem to agree).

And compilers are not required to diagnose this, and e.g. g++ 3.4.2 doesn't.


Quote:
Or is there some way to call such a constructor without specifying
any arguments?

Nope.

PS: If anybody knows, why is "..." permitted for a default constructor
but not for a copy constructor, where optional args must be defaulted?

What do you mean? Why do you think ... is forbidden for a copy constructor?
What does ... have to do with defaulting optional parameters?

Answering question 3 first, "..." specifies an arbitrary number of
optional parameters where defaults cannot be specified, and so it has nothing
to do with defaulting optional parameters.

Answer to question 2: "..." is forbidden for a copy constructor by the
definition of copy constructor, §12.8/2 "either there are no other parameters
or else all other parameters have default arguments", according to C++98. In
the unofficial revisions list for C++2003 §12.8/2 is included but seems to be
identical, letter by letter. I don't have the C++2003 official text.

Answer to question 1: why this difference from a default constructor?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Hyman Rosen
Guest





PostPosted: Wed Mar 23, 2005 1:21 am    Post subject: Re: Multiple default constructors Reply with quote

Alf P. Steinbach wrote:
Quote:
And compilers are not required to diagnose this

They are once a call to a default constructor appears.


Quote:
Answer to question 2: "..." is forbidden for a copy constructor by the
definition of copy constructor, §12.8/2 "either there are no other parameters
or else all other parameters have default arguments", according to C++98.

But ... is not an "other parameter"! In Comeau the following code

class X {
X(const X &, ...) { }
public:
X() { }
};
void f(X) { }
int main() { X x; f(x); }

gives the error

"ComeauTest.c", line 7: error: "X::X(const X &, ...)" is inaccessible
int main() { X x; f(x); }
^

so it clearly thinks that X::X(const X &, ...) is the copy constructor.

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Wed Mar 23, 2005 3:19 am    Post subject: Re: Multiple default constructors Reply with quote

* Hyman Rosen:
Quote:
* Alf P. Steinbach:

And compilers are not required to diagnose this

They are once a [presumably meant: no arguments] call to a default
constructor appears.

Yes, and given that, why is it allowed in the first place, i.e., why should
C++ programmers not be told immediately that what they're doing is meaningless
and will cause a compilation error _later_ if it's ever used?


Quote:
Answer to question 2: "..." is forbidden for a copy constructor by the
definition of copy constructor, §12.8/2 "either there are no other parameters
or else all other parameters have default arguments", according to C++98.

But ... is not an "other parameter"!

You're right in that, and I was wrong: checking it out I found that the
standard implicitly defines "parameter" as formal argument except ellipsis.

[snip]

The question still stands though, why it's defined differently than for
default constructors, and in a way that's prone to misinterpretation?

I suggest a rewording so that those two definitions have the same form,
preferably the one used for default constructor ("can be called without"),


§12.8/10 current text:

"and either there are no other parameters or else all other parameters
have default arguments"


§12.8/10 suggested Replacement text
(same form as for default constructor):

"and it can be called with exactly one actual argument".


and perhaps §12.1/10 should also be made _consistent_ with §12.8:


§12.1/10 current text:

A _copy constructor_ for a class X is a constructor with a first parameter
of type X& or of type const X& [_Note_: see 12.8 for more information on
copy constructors.]


This is inconsistent with §12.8, which additionally allows volatile, and
which excludes templated constructors as copy constructors, so


§12.1/10 suggested Replacement text:

A _copy constructor_ is defined by 12.8.


Those suggestions made in the spirit of always reducing the size and
increasing the clarity, when you have the opportunity... :-)

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Hyman Rosen
Guest





PostPosted: Wed Mar 23, 2005 7:46 pm    Post subject: Re: Multiple default constructors Reply with quote

Alf P. Steinbach wrote:
Quote:
why is it allowed in the first place

C++ and C are full of places where one may write code which is
provably illegal if ever invoked. If you're that concerned about
it, get your vendor to add a warning.

Quote:
The question still stands though, why it's defined differently than for
default constructors, and in a way that's prone to misinterpretation?

It's not all that different. I've never seen anyone misinterpret this
before, so it's not as prone as you seem to think.

Quote:
I suggest a rewording

I doubt many will be willing to rewrite language that already expresses
the correct intent.

Quote:
This is inconsistent with §12.8, which additionally allows volatile, and
which excludes templated constructors as copy constructors, so

§12.1/10 suggested Replacement text:

A _copy constructor_ is defined by 12.8.

This I do like.

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Dave Harris
Guest





PostPosted: Wed Mar 23, 2005 7:46 pm    Post subject: Re: Multiple default constructors Reply with quote

[email]alfps (AT) start (DOT) no[/email] (Alf P. Steinbach) wrote (abridged):
Quote:
Is there any reason why the standard doesn't forbid multiple default
constructors for a class?

Presumably for consistency with the more general rule, eg:

void proc( int x, int y=0 );
void proc( int x, double y=27 );

This is arguably a dumb thing to do, in that a call like:
proc( 42 );

is now ambiguous so neither default argument can be used.

And presumably the general rule is because the declarations of proc may in
general be in different headers. They make sense on their own and it would
be unduly restrictive to say they cannot be brought together.

Constructors differ in that they are already brought together, by virtue
of being in the same class. I guess this wasn't thought significant enough
to make a special case.

-- Dave Harris, Nottingham, UK

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
William M. Miller
Guest





PostPosted: Wed Mar 30, 2005 2:37 am    Post subject: Re: Multiple default constructors Reply with quote

Alf P. Steinbach wrote:
Quote:
and perhaps §12.1/10 should also be made _consistent_ with §12.8:


§12.1/10 current text:

A _copy constructor_ for a class X is a constructor with a first parameter
of type X& or of type const X& [_Note_: see 12.8 for more informationon
copy constructors.]


This is inconsistent with §12.8, which additionally allows volatile, and
which excludes templated constructors as copy constructors, so


§12.1/10 suggested Replacement text:

A _copy constructor_ is defined by 12.8.

Something very similar to this is already done. See
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#331.

--
William M. (Mike) Miller | Edison Design Group, Inc.
[email]william.m.miller (AT) gmail (DOT) com[/email]

---
[ 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 ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.