Topic: Multiple default constructors


Author: Hyman Rosen <hyrosen@mail.com>
Date: Wed, 23 Mar 2005 13:46:08 CST
Raw View
Alf P. Steinbach wrote:
> 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.

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

> I suggest a rewording

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

> 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                       ]





Author: brangdon@cix.co.uk (Dave Harris)
Date: Wed, 23 Mar 2005 19:46:14 GMT
Raw View
alfps@start.no (Alf P. Steinbach) wrote (abridged):
> 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                       ]





Author: william.m.miller@gmail.com ("William M. Miller")
Date: Wed, 30 Mar 2005 02:37:55 GMT
Raw View
Alf P. Steinbach wrote:
> and perhaps =A712.1/10 should also be made _consistent_ with =A712.8:
>=20
>    =20
>   =A712.1/10 current text:
>=20
>   A _copy constructor_ for a class X is a constructor with a first para=
meter
>   of type X& or of type const X& [_Note_: see 12.8 for more information=
 on
>   copy constructors.]
>=20
>=20
> This is inconsistent with =A712.8, which additionally allows volatile, =
and
> which excludes templated constructors as copy constructors, so
>=20
>=20
>   =A712.1/10 suggested Replacement text:
>=20
>   A _copy constructor_ is defined by 12.8.

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

--
William M. (Mike) Miller | Edison Design Group, Inc.
william.m.miller@gmail.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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Hyman Rosen <hyrosen@mail.com>
Date: Tue, 22 Mar 2005 19:21:44 CST
Raw View
Alf P. Steinbach wrote:
> And compilers are not required to diagnose this

They are once a call to a default constructor appears.


> 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                       ]





Author: hyrosen@mail.com (Hyman Rosen)
Date: Tue, 22 Mar 2005 21:26:18 GMT
Raw View
Alf P. Steinbach wrote:
> 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.

> 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?

---
[ 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                       ]





Author: alfps@start.no (Alf P. Steinbach)
Date: Tue, 22 Mar 2005 21:19:08 CST
Raw View
* Hyman Rosen:
> * 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?


> > 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                       ]





Author: alfps@start.no (Alf P. Steinbach)
Date: Tue, 22 Mar 2005 23:05:03 GMT
Raw View
* Hyman Rosen:
> * 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.


> > 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                       ]





Author: alfps@start.no (Alf P. Steinbach)
Date: Tue, 22 Mar 2005 20:40:32 GMT
Raw View
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                       ]