私は関数のテンプレート型をコンパイル時に特定のクラスとそのサブクラスに制限しようとしています。これを実現するために、私はタイプを使用していますと、このようstd::enable_if_t
とstd::is_base_of
を特色:SFINAEのstd :: enable_if_t&std :: is_base_ofを使ってコンパイル時の継承をチェックする方法は?
template <typename T = std::enable_if_t<std::is_base_of<A, T> > >
しかし、テンプレートは、まだ継承階層(すなわちint
)の一部ではないタイプでコンパイルします。以下は、問題のMCVEです:
class A {
public:
A(float a) : a(a) {}
float a;
};
class B : public A{
public:
B(float a) : A(a) {}
};
template <typename T = std::enable_if_t<std::is_base_of<A, T> > >
void templateFunction(T a) {
}
int main() {
templateFunction<A>(A(1.0f)); // OK -> std::is_base_of<A, A>
templateFunction<B>(B(1.0f)); // OK -> std::is_base_of<A, B>
templateFunction<int>(1); // Should not compile! int is not a subclass of A -> std::is_base_of<A, int>
return 0;
}
これはエラーなしのVisual Studio 2017の下でコンパイルしますが、テンプレート関数の最後のインスタンス化は、私の理解でコンパイルべきではありません。 私のタイプ特性の使用に問題はありますか、Visual Studio SFINAEの実装に問題がありますか?