我有以下代码:C++模板类的继承[无法转换模板]
#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;
}
为什么我不能叫 “T” 就这样?
t<C1 , int, &C1::a>();
但是,相反,我得到以下错误:
Could not convert template argument ‘&Base<1>::a’ to ‘int C1::*’
附:我能理解这种行为,如果C1是Typedef的...
嗯......对于初学者没有基础,以衍生转换在成员指针它为什么做,所以这是很好不会自动地做,但我发现你甚至不能施放它。 – uk4321
@Mike - 为什么他们必须是静态成员?这是指向成员的指针http://msdn.microsoft.com/en-us/library/k8336763.aspx – tower120
@ uk4321 - “在成员指针上不进行基于派生的转换” - 这是关于什么的? “但我发现你甚至不能施展它” - 你是什么意思? – tower120