Topic: Defect report: Pointers to inherited members


Author: Lisa Lippincott <lisa_lippincott@bigfix.com>
Date: 2000/02/09
Raw View
 [Moderator's note: this defect report has been forwarded to the
 C++ committee.  The two recently posted defect reports without
 notes like this have also been forwarded to the committee,
 I just forgot to add the notes.  -moderator (fjh).]

I used to think that the type of "&X::Y" was a pointer to a member of X.
That idea had an elegant simplicity, and a certain utility -- templates
could infer the type X from the pointer.

But when inheritance is involved, the normative text of the standard
(5.3.1, expr.unary.op) is less than clear:

      If the member is a nonstatic member of class C of type T, the
      type of the result is `pointer to member of class C of type T.'

There is no clear referrent for "class C" when the member belongs
by inheritance to more than one class.  But there is an unfortunate
example:

      struct A { int i; };
      struct B : A { };
      ... &B::i ...          // has type int A::*

This allows certain bizarre code:

      struct C : A {};
      int (C::*p) = &B::i;

And makes templates act strangely:

      struct A
        {
         int i;
         virtual void f() = 0;
        };

      struct B : A
        {
         int j;
         B() : j(5)  {}
         virtual void f();
        };

      struct C : B
        {
         C()      { j = 10; }
        };

      template < class T >
      int DefaultValue( int (T::*m) )
        {
         return T().*m;
        }

      ... DefaultValue( &B::i )    // Error: A is incomplete.
      ... DefaultValue( &C::j )    // returns 5, not 10.

I think the quoted sentence of the standard should be rewritten

   If the member is a nonstatic member of qualified-id of type T, the
   type of the result is `pointer to member of qualified-id of type T.'

and the comment in the example changed to match.

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