Topic: Bad design when deriving from std::string?


Author: jpotter@falcon.lhup.edu (John Potter)
Date: 1998/01/19
Raw View
kmark@pacbell.x.net (Mark Williams) wrote:

: In article <69nq2l$7e9@netlab.cs.rpi.edu>, bonnardv@pratique.fr wrote:

: > Mark Williams <kmark@pacbell.x.net> writes:

: > > void foo( int i )
: > > {
: > >    MyClass mc1;
: > >    const MyClass &mc2 = i ? MyClass() : mc1;
: > >
: > >    throwing_func();
: > > }

[ Is lifetime of the temp the expression (bonnard) or the function foo
(williams)? ]

: In anycase, we can avoid these quibbles by changing the example a little...

: throwing_func(i ? MyClass : mc1);

: And as I said before, although you cant tell from the pc alone
: whether or not to destroy it, the same problem occurs with or
: without exceptions... you need some kind of flag. So exception
: support does not _require_ extra code to handle this case.

Finding the question interesting, not being sure of the CD2 statements
and not seeing any answers here, I consulted my compilers.

Comp1 used old rules and did not support exceptions.  It destucted the
temporary at the end of the function iff it was constructed.

Comp2 destructed the temporary at the end of the expression.  It also
redestructed whatever was bound to mc2 at the end of the function.

Comp3 refused the code since a destruction was required in a
conditional.

Comp4 destructed the temporary at the end of the expression for mc2.
It crashed and burned on the conditional parameter (the compiler not
the test program).

Is comp3 correct?  If not, what is the lifetime of the temporary in
the first conditional?

John
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: kmark@pacbell.x.net (Mark Williams)
Date: 1998/01/17
Raw View
In article <69nq2l$7e9@netlab.cs.rpi.edu>, bonnardv@pratique.fr wrote:

> [Followups to comp.std.c++]
>
> Mark Williams <kmark@pacbell.x.net> writes:
>
> > I think Alfred was looking for something more along the lines of
> >
> > void foo( int i )
> > {
> >    MyClass mc1;
> >    const MyClass &mc2 = i ? MyClass() : mc1;
> >
> >    throwing_func();
> > }
> >
> > [From memory, the lifetime of the temporary, if created, is the same as
> > the reference it gets bound to... ie the end of the function]
>
> Which function ? Without checking the std, I think it's at the
> end of the ?: lazy operator, so it's destroyed immediatly, just
> as in:

Well, according to 12.2 Temporary Objects paragraph 5, a temporary bound
to a reference persists until the end of the scope in which it was
created, or the end of the lifetime of the reference object, whichever
comes sooner.

>
> template <class T>
> const T& id (const T& x)
> {
>     return x;
> }
>
> const int& drci = id (int ());
>
> rci is dandling immediatly after creation, but the following
> is ok:

Agreed, because the temporary is bound to id's parameter x, so its
lifetime is the same as that of x.

>
> const int& rci = int();
>
> cout << rci; // prints 0

Again, because the temporary gets the same lifetime as
the reference its bound to...

In anycase, we can avoid these quibbles by changing the example a little...

throwing_func(i ? MyClass : mc1);

(assuming a suitable declaration for throwing_func)

Now we definitely have an object which may or may not have been
constructed, and has not been destroyed, at the point that
throwing_func throws... which is what the original poster was
looking for.

And as I said before, although you cant tell from the pc alone
whether or not to destroy it, the same problem occurs with or
without exceptions... you need some kind of flag. So exception
support does not _require_ extra code to handle this case.

Mark Williams
---
[ 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         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]