Topic: Q: pointer-to-member conversions


Author: "Lassi A. Tuura" <Lassi.Tuura@cern.ch>
Date: 1996/07/25
Raw View
I could not find any real resolution from the working paper to this
question, so could someone knowledgeable enough please clarify it?
One compiler I use (KAI C++ 2.05c) barks, others (g++ 2.7.2, HP aCC
beta, HP CC) compile it happily.

The code:
----------------------------------------
struct A { int i; };
struct B : A { int j; };
struct C { B b; };

int main (int, char **)
{
    A C::*ptr = &C::b;          // (*)
    return 0;
}
----------------------------------------

The conversion in question is the line marked with (*): is it legal to
convert `D T::*' to `B T::*', where `B' is an accessible base class of
`D'?  The sections that seem relevant (and quoted to me by KAI's
support),
  - 4.11 [conv.mem],
  - 5.2.8 [expr.static.cast] para 9,
  - 5.2.9 [expr.reinterpret.cast] para 10
do not say a word about a conversion like this.  Am I then right to
assume that since it not mentioned, it is not legal, and the other
compilers are in error?

If this conversion is indeed illegal, is there any other well-defined
way to accomplish the same thing?  `static_cast' seems to disallow the
conversion and `reinterpret_cast' does not seem to guarantee correct
behaviour.  One solution I thought about was to do conversion after
applying the member pointer, but this complicates things quite a lot.

//lat
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: bill@gibbons.org (Bill Gibbons)
Date: 1996/07/26
Raw View
In article <kspybk861go.fsf@hpmoose6.cern.ch>, "Lassi A. Tuura"
<Lassi.Tuura@cern.ch> wrote:

> I could not find any real resolution from the working paper to this
> question, so could someone knowledgeable enough please clarify it?
> One compiler I use (KAI C++ 2.05c) barks, others (g++ 2.7.2, HP aCC
> beta, HP CC) compile it happily.
>
> The code:
> ----------------------------------------
> struct A { int i; };
> struct B : A { int j; };
> struct C { B b; };
>
> int main (int, char **)
> {
>     A C::*ptr = &C::b;          // (*)
>     return 0;
> }
> ----------------------------------------
>
> The conversion in question is the line marked with (*): is it legal to
> convert `D T::*' to `B T::*', where `B' is an accessible base class of
> `D'?

No.  The only conversion allowed on the object type portion of a pointer
to member is changing the const/volatile qualification.

I don't know about the other compilers, but the HP aCC beta compiler had
this conversion from before the rules were clarified.  It was supposed
to have been removed (and now it will be).

--
Bill Gibbons
bill@gibbons.org
---
[ 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                             ]