1

I have the following code:

#include <iostream>
using namespace std;

template<int k>
struct Base{
   int a = k;
};

struct C1 : Base<1>{};
struct C2 : Base<2>{int b;};

typedef Base<1> C1T;


    template<
            typename BufferType,

            typename VerticesType,
            VerticesType BufferType::* VerticesField = nullptr
            >
    void t(){};


int main() {
// WHY this work??
    t<C1T , int, &C1T::a>();

// And this not?
// t<C1 , int, &C1::a>();

// ok.
//  t< Base<1>, int, &Base<1>::a >();

// also, ok
t<C2 , int, &C2::b>();
    return 0;
}

http://ideone.com/8tWCJS

Why I can't call "t" in this way ?

t<C1 , int, &C1::a>();

But instead, I get the following error:

Could not convert template argument ‘&Base<1>::a’ to ‘int C1::*’

P.S. I could understand this behavior if C1 was typedef'ed...

tower120
  • 5,007
  • 6
  • 40
  • 88
  • Hmm... well for starters no base-to-derived conversion is done on member pointers, so that's why it's not automatically doing it, but I found that you can't even cast it. – uk4321 Dec 22 '13 at 18:28
  • @Mike - why they have to be static members? This is pointer's to members http://msdn.microsoft.com/en-us/library/k8336763.aspx – tower120 Dec 22 '13 at 18:32
  • @uk4321 - "no base-to-derived conversion is done on member pointers" - Where you this about? "but I found that you can't even cast it" - what do you mean? – tower120 Dec 22 '13 at 18:35
  • 1
    @tower120 read http://stackoverflow.com/a/4027336/2925619 and "you can't even cast it" means I found that member pointer template params have to be in the form `&X::Y` (according to GCC) so you can't do a cast in there. – uk4321 Dec 22 '13 at 18:47
  • @uk4321 "I found that member pointer template params have to be in the form &X::Y" - and which form in my example? – tower120 Dec 22 '13 at 20:26
  • I'm wondering if taking the address of a non-static member (Base::a) is legal without having an instance pointer. AFAICT, Base::a should be static int in order to get a valid &[Base or derived]::a pointer. – valir Dec 23 '13 at 08:55
  • @valir - Why??? Why it should be static? if this is pointer-to-member msdn.microsoft.com/en-us/library/k8336763.aspx – tower120 Dec 23 '13 at 09:11

1 Answers1

0

Try:

t<Base<1> , int, &C1::a>();
  • @Kate It's right in the error message to be honest. This and `t< Base<1>, int, &Base<1>::a >();` are equivalent. –  Dec 22 '13 at 18:52
  • Ok - I updated my question with typedef variant. Why it works with typedef and not work with inheritance? – tower120 Dec 22 '13 at 19:40