f



Pointer to a pointer to a pointer to a member function

Hi,

I'm having a problem compiling the following snippet with MSVC++ 7.1
and I'm not sure if it is a compiler bug? (or I am being stupid)

struct Vector3
{
        float x, y, z;
};

void blah()
{
        static Vector3 (Vector3::*function)(float);
        static Vector3 (Vector3::**_function)(float) = &function;
        /*static*/ Vector3 (Vector3::***__function)(float) = &_function;

        *__function = &function;
}

If I uncomment the 'static' qualifier it then compiles fine? (Or if I
change the return type of the function pointers to an atomic type such
as 'int') The error I get from the compiler is:

error C2440: 'initializing' : cannot convert from 'Vector3 (__thiscall
Vector3::* **  )(float)' to 'Vector3 (__thiscall Vector3::* **
)(float)'
        Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast

which I guess is somewhat amusing(!).


In case you wonder why I am using such levels of indirection it is
because I am doing something like the following:

template<unsigned ID, class Class>
inline static void MemberFunction(void (Class::***pointer)())
{
        static void (Class::*function)();


        *pointer = &function;
}

for some template meta programming binding stuff - it's basically a
singleton accessor function.  I did originally have it as:

template<unsigned ID, class Class, class ReturnType>
inline static /*typename*/ ReturnType (Class::**)() MemberFunction()
{
        static ReturnType (Class::*function)();


        return &function;
}

but I couldn't get the compiler to like the return type of
'MemberFunction'.

Any ideas?

Thanks,

Warrick.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
warrick (1)
4/12/2004 9:32:53 AM
comp.lang.c++.moderated 10738 articles. 1 followers. allnor (8509) is leader. Post Follow

4 Replies
718 Views

Similar Articles

[PageSpeed] 8

warrick@chimeric.co.uk (Warrick) writes:

> Hi,
>
> I'm having a problem compiling the following snippet with MSVC++ 7.1
> and I'm not sure if it is a compiler bug? (or I am being stupid)
>
> struct Vector3
> {
>         float x, y, z;
> };
>
> void blah()
> {
>         static Vector3 (Vector3::*function)(float);
>         static Vector3 (Vector3::**_function)(float) = &function;
>         /*static*/ Vector3 (Vector3::***__function)(float) = &_function;
>
>         *__function = &function;
> }
>
> If I uncomment the 'static' qualifier it then compiles fine?

Names containing double underscores, like those beginning with an
underscore and a capital letter, or any name beginning with an
underscore at global scope, are reserved to the implementation.  It
wouldn't surprise me at all if your compiler had some special meaning
for the name "__function".  Try using legal names.

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
David
4/12/2004 6:29:09 PM
Hi David,

Point taken(!) but unfortunately even with legal names it doesn't
work.  I've resorted to having a reinterpret_cast in there which is
evil but actually makes it all work fine.

So I'm assuming that it's a compiler bug and living with the nasty
hack for the moment.

Thanks,

Warrick.

David Abrahams <dave@boost-consulting.com> wrote in message news:<uad1hifp6.fsf@boost-consulting.com>...
 > warrick@chimeric.co.uk (Warrick) writes:
 >
 > > Hi,
 > >
 > > I'm having a problem compiling the following snippet with MSVC++ 7.1
 > > and I'm not sure if it is a compiler bug? (or I am being stupid)
 > >
 > > struct Vector3
 > > {
 > >         float x, y, z;
 > > };
 > >
 > > void blah()
 > > {
 > >         static Vector3 (Vector3::*function)(float);
 > >         static Vector3 (Vector3::**_function)(float) = &function;
 > >         /*static*/ Vector3 (Vector3::***__function)(float) = &_function;
 > >
 > >         *__function = &function;
 > > }
 > >
 > > If I uncomment the 'static' qualifier it then compiles fine?
 >
 > Names containing double underscores, like those beginning with an
 > underscore and a capital letter, or any name beginning with an
 > underscore at global scope, are reserved to the implementation.  It
 > wouldn't surprise me at all if your compiler had some special meaning
 > for the name "__function".  Try using legal names.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
warrick
4/13/2004 8:03:54 AM
David Abrahams wrote:
 > warrick@chimeric.co.uk (Warrick) writes:
 >> If I uncomment the 'static' qualifier it then compiles fine?
 >
 > Names containing double underscores, like those beginning with an
 > underscore and a capital letter, or any name beginning with an
 > underscore at global scope, are reserved to the implementation.  It
 > wouldn't surprise me at all if your compiler had some special meaning
 > for the name "__function".  Try using legal names.

True, but not related to the problem in this case.  It's a bug - still
present in the VC8 alpha, so I'll report it to MS.

You can use typedefs as a workaround to get the original code to parse.

-cd


      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Carl
4/13/2004 8:06:25 AM
[Please don't top-post.]

Warrick wrote:
 > David Abrahams <dave@boost-consulting.com> wrote in message
 > news:<uad1hifp6.fsf@boost-consulting.com>...
 > > warrick@chimeric.co.uk (Warrick) writes:
<snip>
 > > > I'm having a problem compiling the following snippet with MSVC++ 7.1
 > > > and I'm not sure if it is a compiler bug? (or I am being stupid)
 > > >
 > > > struct Vector3
<snip>
 > > > void blah()
 > > > {
 > > >         static Vector3 (Vector3::*function)(float);
 > > >         static Vector3 (Vector3::**_function)(float) = &function;
 > > >         /*static*/ Vector3 (Vector3::***__function)(float) = &_function;
 > > >
 > > >         *__function = &function;
 > > > }
 > > >
 > > > If I uncomment the 'static' qualifier it then compiles fine?
 > >
 > > Names containing double underscores, like those beginning with an
 > > underscore and a capital letter, or any name beginning with an
 > > underscore at global scope, are reserved to the implementation.
<snip>
 > Point taken(!) but unfortunately even with legal names it doesn't
 > work.  I've resorted to having a reinterpret_cast in there which is
 > evil but actually makes it all work fine.
 >
 > So I'm assuming that it's a compiler bug and living with the nasty
 > hack for the moment.

It looks like there's some weirdness in the parser that causes it to
build slightly different type representations in the parse tree.
There's probably a flag that's being ignored when printing out the
type in the error message but is not when type-checking.

A better work-around would be to use a type alias:

     typedef Vector3 (Vector3::* pmf_type)(float);

This works for me with the above compiler.

      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
0
Ben
4/14/2004 6:38:26 AM
Reply: