c++/1841: Incorrect error: invalid use of member '' in staticmember function

Martin Sebor sebor@roguewave.com
Fri Feb 2 16:43:00 GMT 2001


Jean-Bernard Delva wrote:
> 
> > In my experience MSVC compiling something tells you very little about
> > its correctness, unless you're talking about -ve correlations :-)
> 
> Agreed.
> 
> That is why I compiled it with the Diab compiler (a well-repected compiler
> for embedded systems) and I get the same results.
> 
> > > struct X
> > > {
> > >     static int X::* foo () { return &x; };
> > >
> > >     int x;
> > > };
> >
> > Maybe you could explain what this means?  I don't think it has a
> > meaning.  If a member function is static, it applies to the class as a
> > type, and not any specific instance.  So the 'x' of which instance are
> > you trying to return a pointer to?
> 
> I understand that this construct is bordering on the isoteric but we need
> code that uses this. And after analysis, it seems correct.

The code is incorrect, the type of the result is int*, the type returned from
the function is int X::*. Here's the relevant text:

5.3.1, p2: "The result of the unary & operator is a pointer to its operand. The
operand shall be an lvalue or a qualified-id. In the first case, if the type of
the expression is ``T,'' the type of the result is ``pointer to T.'' In
particular, the address of an object of type ``cv T'' is ``pointer to cv T,''
with the same cv-qualifiers. For a qualified-id, if the member is a static
member of type ``T'', the type of the result is plain ``pointer to T.'' 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.'

Regards
Martin


More information about the Gcc-bugs mailing list