Topic: Forwarding ctors (was: Two Proposals for the C++ Standard)


Author: David R Tribble <dtribble@technologist.com>
Date: 1999/02/14
Raw View
Steve Clamage wrote:
> Lawrence Crowl has suggested the language be extended to allow
> "forwarding constructors".  Example:
>         class T {
>                 T(int);         // the "real" constructor
>                 T() : T(3) { }  // forwarding constructor
>         };
> This pair of constructors would have the effect of
>                 T(int=3)
> I don't see any problems with this extension, although one
> compiler writer said he didn't see how to implement it. (When
> this particular person can't see a way to implement it, I assume
> I have missed something.) If feasible, it would go a long way to
> elmininating the need for default arguments.

The fact that T() refers to T(int) should be enough of a clue for
the compiler to know to call T(int) and then call T() when the
programmer invokes T().  I don't see the flaw in treating T(int)
like a base constructor for T().

Of course, the programmer would have to avoid recursion among
the constructors (which could be detected by the compiler).

-- David R. Tribble, dtribble@technologist.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://reality.sgi.com/austern_mti/std-c++/faq.html              ]