template<typename T> struct A {
auto func() -> decltype(T::func()) {
return T::func();
}
};
class B : public A<B> {
void func() {
}
};
私にはかなり単純なようです。しかし、MSVCはコンパイルに失敗します。
visual studio 2010\projects\temp\temp\main.cpp(4): error C2039: 'func' : is not a member of 'B'
visual studio 2010\projects\temp\temp\main.cpp(8) : see declaration of 'B'
visual studio 2010\projects\temp\temp\main.cpp(8) : see reference to class template instantiation 'A<T>' being compiled
with
[
T=B
]
visual studio 2010\projects\temp\temp\main.cpp(4): error C3861: 'func': identifier not found
コンパイラーは関数の呼び出しを喜んで受け入れますが。以下のサンプルは正常にコンパイルされます。
template<typename T> struct A {
void func() {
return T::func();
}
};
class B : public A<B> {
void func() {
}
};
テンプレート引数から任意の型を使用しようとすると、同じ問題が発生します。
template<typename T> struct A {
typedef typename T::something something;
};
class B : public A<B> {
typedef char something;
};
visual studio 2010\projects\temp\temp\main.cpp(4): error C2039: 'something' : is not a member of 'B'
一方、クラスBは「何か」と呼ばれるタイプを明確に定義しています。コンパイラーは、T型、T&型、またはT *型のオブジェクトで関数を呼び出すことができますが、Tからどの型にもアクセスできないようです。