Topic: Semantics of const *&
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/05/05 Raw View
schmidt@tango.cs.wustl.edu (Douglas C. Schmidt) writes:
[edited slightly for brevity:]
>class Baz {};
>class Bar : public Baz {};
>
>class Foo {
>public:
> Bar *rfile;
> Baz *const &ref;
Hmm, the subject line is a bit confusing - this is `*const &',
not the `const *&' that the subject line talks about.
The two are quite different - the former is a reference to
a const pointer to a non-const Baz, the latter would be
a reference to a non-const pointer to const Baz.
> Foo (void): ref (rfile)
Here `ref' is a reference to a constant pointer to a non-const Baz,
and `rfile' is a pointer to a non-const Bar. Now a pointer to
a non-const Bar can be converted to a pointer to a non-const Baz,
since Bar is derived from Baz; furthermore, a reference to constant X
can always be initialized with anything that can be converted
to X; hence this initialization is ok. However, `ref' will refer
to the result of the conversion, which will be a temporary, and
the lifetime of that temporary is rather short - it only lasts
until the end of the constructor, so this is probably not doing
what you want.
[References: 8.5.3/4-8; 12.2/5.]
> {
> rfile = new Bar;
> assert (rfile == ref);
At this point, the constructor has not finished executing, so the
temporary is still alive. (If the assert was after the constructor
had finished, you would be dereferencing a dangling reference,
and so you would get undefined behaviour.)
But `ref' still refers to the temporary, not to `rfile', and so
the assertion must fail.
> Although this compiles with Sun C++ 4.0.1 and gcc it seems
>that the initialization
>
> Foo (void): ref (rfile)
>
>should be rejected since ref has been defined to refer to a const *,
>whereas rfile is a non-const *.
No, you're confusing `*const &' with `const *&'.
> Interestingly, when compiled and run, this code fails the
>assertion on Sun C++ 4.0.1. However, it runs fine with gcc.
It sounds like gcc does not conform to the latest working paper.
--
Fergus Henderson | I'll forgive even GNU emacs as
fjh@cs.mu.oz.au | long as gcc is available ;-)
http://www.cs.mu.oz.au/~fjh | - Linus Torvalds
Author: schmidt@tango.cs.wustl.edu (Douglas C. Schmidt)
Date: 1995/05/01 Raw View
Hi,
A student asked me the other day whether the following code
was legal C++:
----------------------------------------
#include <assert.h>
class Baz
{
public:
Baz (void) {}
};
class Bar : public Baz
{
public:
Bar (void) {}
};
class Foo
{
public:
Bar *rfile;
Baz *const &ref;
Foo (void): ref (rfile)
{
rfile = new Bar;
assert (rfile == ref);
}
};
int main ()
{
Foo foo;
}
----------------------------------------
Although this compiles with Sun C++ 4.0.1 and gcc it seems
that the initialization
Foo (void): ref (rfile)
should be rejected since ref has been defined to refer to a const *,
whereas rfile is a non-const *.
Interestingly, when compiled and run, this code fails the
assertion on Sun C++ 4.0.1. However, it runs fine with gcc.
Can anyone shed any light on this?
Thanks,
Doug
--
Dr. Douglas C. Schmidt (schmidt@cs.wustl.edu)
Department of Computer Science, Washington University
St. Louis, MO 63130. Work #: (314) 935-7538; FAX #: (314) 935-7302
http://www.cs.wustl.edu/~schmidt/
Author: schmidt@tango.cs.wustl.edu (Douglas C. Schmidt)
Date: 1995/05/01 Raw View
Thanks to John Spicer of Edison Design Group I know have a solution to
my previous question about const *&:
----------------------------------------
#include <assert.h>
class Baz
{
public:
Baz () {}
};
class Bar : public Baz
{
public:
Bar (void) {}
};
class Foo
{
public:
Bar *rfile;
Baz *const &ref;
Foo (void): rfile (new Bar), ref (&(Baz &) *rfile)
{
assert (rfile == ref);
}
};
int main ()
{
Foo foo;
}
----------------------------------------
Doug
--
Dr. Douglas C. Schmidt (schmidt@cs.wustl.edu)
Department of Computer Science, Washington University
St. Louis, MO 63130. Work #: (314) 935-7538; FAX #: (314) 935-7302
http://www.cs.wustl.edu/~schmidt/