Topic: Return from ctor???


Author: David R Tribble <dtribble@technologist.com>
Date: 1999/02/19
Raw View
Siemel Naran wrote:
>> A constructor can be thought of as a function that returns void.
>> A constructor can also be thought of as a function that returns
>> *this.

David R Tribble <dtribble@technologist.com> wrote:
>> Except that constructors can't be declared like normal functions.
>> The same goes for destructors.
>>
>> I have argued in the past for making it legal to declare a ctor
>> as returning 'Type', and to declare a dtor as returning 'void'.

Siemel Naran wrote:
> But what this does buy you?

A little more symmetry in the language.

David:
>>Consider:
>>    class Type
>>    {
>>    public:
>>        Type    Type();     // ctor
>>        void    ~Type();    // dtor
>>    };

Siemel:
> [The following] code is already legal in C++.

>>    void foo()
>>    {
>>        bar(Type());        // Calls Type::Type(),
>>                            // which "returns" a Type object
>>
>>        Type *  p = ...;
>>        p->~Type();         // Calls Type::~Type(),
>>                            // which returns nothing
>>    }

Yes, it is legal, and it serves to illustrate my point about
symmetry.  We can "call" a constructor, and we can call a
destructor, which is certainly function-like behavior, so why not
allow them to be declared like functions, with return types?
I still don't see the harm in it.

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






Author: Biju Thomas <bijuthom@ibm.net>
Date: 1999/02/15
Raw View
Siemel Naran wrote:
>
> On 13 Feb 1999 00:20:02 GMT, David R Tribble <dtribble@technologist.com> wrote:
> >Consider:
> >    class Type
> >    {
> >    public:
> >        Type    Type();     // ctor
> >        void    ~Type();    // dtor
> >    };
>
> This code is already legal in C++.

No, it is not. A constructor or destructor cannot have any return type
in C++. (See paragraphs 12.1/12 and 12.4/2 in the standard.)

--
Regards,
Biju Thomas
---
[ 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              ]





Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1999/02/15
Raw View
On 15 Feb 99 14:22:42 GMT, Biju Thomas <bijuthom@ibm.net> wrote:
>Siemel Naran wrote:

>> >    class Type
>> >    {
>> >    public:
>> >        Type    Type();     // ctor
>> >        void    ~Type();    // dtor
>> >    };
>>
>> This code is already legal in C++.
>>[snip]

>No, it is not. A constructor or destructor cannot have any return type
>in C++. (See paragraphs 12.1/12 and 12.4/2 in the standard.)

Sorry, my mistake again (not being specific enough).  The sentence
"this code is already legal in C++" was referring to the code below
the sentence, which I've marked as "snip".

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


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






Author: David R Tribble <dtribble@technologist.com>
Date: 1999/02/13
Raw View
Marc Girod <girod@stybba.ntc.nokia.com> wrote:
>> Is it legal to return from a constructor? What is the behaviour?

Siemel Naran wrote:
> Yes, it is legal.

> A constructor can be thought of as a function that returns void.
> A constructor can also be thought of as a function that returns *this.

Except that constructors can't be declared like normal functions.
The same goes for destructors.

I have argued in the past for making it legal to declare a ctor
as returning 'Type', and to declare a dtor as returning 'void'.

Consider:
    class Type
    {
    public:
        Type    Type();     // ctor
        void    ~Type();    // dtor
    };

    void foo()
    {
        bar(Type());        // Calls Type::Type(),
                            // which "returns" a Type object

        Type *  p = ...;
        p->~Type();         // Calls Type::~Type(),
                            // which returns nothing
    }

I didn't see the harm in this, and it has the benefit of allowing
all function declarations to have an explicit return type (adding
a little more symmetry to the language).  But most people rejected
this idea.

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






Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1999/02/14
Raw View
On 13 Feb 1999 00:20:02 GMT, David R Tribble <dtribble@technologist.com> wrote:
>Siemel Naran wrote:

>> A constructor can be thought of as a function that returns void.
>> A constructor can also be thought of as a function that returns *this.

>Except that constructors can't be declared like normal functions.
>The same goes for destructors.
>
>I have argued in the past for making it legal to declare a ctor
>as returning 'Type', and to declare a dtor as returning 'void'.

But what this does buy you?

>Consider:
>    class Type
>    {
>    public:
>        Type    Type();     // ctor
>        void    ~Type();    // dtor
>    };


This code is already legal in C++.

>    void foo()
>    {
>        bar(Type());        // Calls Type::Type(),
>                            // which "returns" a Type object
>
>        Type *  p = ...;
>        p->~Type();         // Calls Type::~Type(),
>                            // which returns nothing
>    }
>
>I didn't see the harm in this, and it has the benefit of allowing
>all function declarations to have an explicit return type (adding
>a little more symmetry to the language).  But most people rejected
>this idea.

Well, one more thing.  Constructors aren't really functions.  This
means that you can't call a constructor on an already constructed
object.  It also means that you can't take the address of a
constructor.  Being able to take the address of a constructor is
pretty much equivalent to writing a template, eg:
   void f(address of 1-arg constructor called ctor) {
      ctor object(3);
   }
But in C++ one writes:
   template <class T> void f() {
      T object(3);
   }

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


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






Author: Marc Girod <girod@stybba.ntc.nokia.com>
Date: 1999/02/10
Raw View
Hello!

Is it legal to return from a constructor? What is the behaviour?

Both egcs 2.93.06 (19990208 snapshot) and HP aCC 1.18 accept the
following code:

struct A {
    A() { return; }
};

Best Regards!
Marc

--
Marc Girod                Hiomo 5/1          Voice:  +358-9-511 23746
Nokia Telecommunications  P.O. Box 320       Mobile: +358-40-569 7954
NWS/NMS/NMS for Data      00045 NOKIA Group  Fax:    +358-9-511 23580
                          Finland            marc.girod@ntc.nokia.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              ]





Author: sbnaran@bardeen.ceg.uiuc.edu (Siemel Naran)
Date: 1999/02/11
Raw View
On 10 Feb 99 21:14:18 GMT, Marc Girod <girod@stybba.ntc.nokia.com> wrote:

>Is it legal to return from a constructor? What is the behaviour?

Yes, it is legal.
A constructor can be thought of as a function that returns void.
A constructor can also be thought of as a function that returns *this.

--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ 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              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1999/02/11
Raw View
Marc Girod wrote:
>
> Hello!
>
> Is it legal to return from a constructor? What is the behaviour?

Yes - in fact failing to return from a constructor is a fairly rare
event. Reaching the end of a constructor body is exactly equivalent to
executing "return;", just like a void function. The object constructed
ends up with whatever values in it that the constructor put there before
returning.
---
[ 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              ]





Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1999/02/11
Raw View
Marc Girod <girod@stybba.ntc.nokia.com> writes:

>Is it legal to return from a constructor?

Yes, as long as you don't try to return a value.

>What is the behaviour?

The same as in any other kind of function.

>Both egcs 2.93.06 (19990208 snapshot) and HP aCC 1.18 accept the
>following code:

>struct A {
>    A() { return; }
>};

That's good, because the code is valid and well-defined.

--
Steve Clamage, stephen.clamage@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    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Biju Thomas <bijuthom@ibm.net>
Date: 1999/02/11
Raw View
Marc Girod wrote:
>
> Is it legal to return from a constructor? What is the behaviour?
>
> Both egcs 2.93.06 (19990208 snapshot) and HP aCC 1.18 accept the
> following code:
>
> struct A {
>     A() { return; }
> };

Yes, it is legal.

Otherwise, we won't be able to write code which does early return from
within constructors. For example:

struct A {
  A ( int x )
  {
    if ( x > 100 )
    {
      return;
    }
    // Do something really serious here.
  }
};

Now, I will try to avoid writing such code violating Dijkstra's
commandmends, if possible. But, sometimes, they look like more readable.

--
Regards,
Biju Thomas


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






Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/02/11
Raw View
In article <1yemnyoufc.fsf@sinkku.ntc.nokia.com>, Marc Girod
<girod@stybba.ntc.nokia.com> writes
>Hello!
>
>Is it legal to return from a constructor? What is the behaviour?

definitely.  Though I would not encourage it, some programmers have even
been known to have multiple returns from a ctor.

>
>Both egcs 2.93.06 (19990208 snapshot) and HP aCC 1.18 accept the
>following code:
>
>struct A {
>    A() { return; }
>};
>
>Best Regards!
>Marc

Francis Glassborow      Chair of Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA          +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


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