views:

266

answers:

3

Consider the following code:

template<class T, class F>           struct X {};
template<class T, class F, T F::* m> struct Y {};

struct Foo {
    int member;
    typedef X<int, Foo>               x_type; // works well
    typedef Y<int, Foo, &Foo::member> y_type; // ERROR
};

typedef Y<int, Foo, &Foo::member> y_type2; // OK

Why does compiler generate error? (VS2008)


New

I have posted this bug to connect.microsoft.com.

+4  A: 

I think that it is related somehow with that Visual C++ don't know the size of pointer to member at that point. Check this defect report for instance (here is another problem with pointer to member variable). I think that you found one more Visual C++ bug and it should be reported to connect.microsoft.com.

Kirill V. Lyadvinsky
I coincide with the 'bug' theory. I stumbled upon the same problem lately, and found - but don't remember where - that they didn't yet 'fully support' pointer-to-member template class arguments.
xtofl
However, the compiler doesn't need to know the _size_, imho.
xtofl
Kirill V. Lyadvinsky
And it is may be (I'm not sure) important for Visual C++. It is just a theory.
Kirill V. Lyadvinsky
xtofl
My idea is that it is bug in implementation. I don't know why it is there :)
Kirill V. Lyadvinsky
Found the bug report: http://support.microsoft.com/kb/249045. Applies to VC6.0, they say...
xtofl
Not relevant. This particular behavior occurs with VC++ with pointers to member **functions**. This example is data. The cause of the difference is that a pointer to method can point to a virtual function, but there is no virtual data.
MSalters
Added link to problem with pointer to member variable.
Kirill V. Lyadvinsky
A: 

I stumbled upon the same problem. The support for pointer-to-member template arguments is still limited in VC++ (see bug report).

In my case I could work around it by using a template function i.s.o. a template class:

template< typename Class > struct CMemberDumper {
    Class& object;
    template< typename M > void visit_member( M C::*pm ) {
       std::cout << object.*pm;
    }
};
xtofl
Your bug is not related to my problem. It works well without any compiler errors. I use VS2008.
Alexey Malistov
A: 

This is a bug

Alexey Malistov
That what I said (http://stackoverflow.com/questions/1802204/i-can-not-get-access-to-pointer-to-member-why/1802365#1802365)
Kirill V. Lyadvinsky