Просто интересно, почему я не могу использовать is_base_of в аргументе шаблона C++ следующим образом:
#include <iostream>
#include <type_traits>
using namespace std;
struct base
{
};
struct derived : public base
{
int value;
};
template<class A, class B, bool isbaseof = std::is_base_of<A,B>::value> myclass
{
void foo() {
cout << "Generic..";
}
};
template<class A, class B> myclass<A,B,true>
{
void foo() {
cout << "A is not base of B";
}
};
int main() {
return 0;
}
Ошибка:
prog.cpp:17:73: error: ‘myclass does not name a type
template<class A, class B, bool isbaseof = std::is_base_of<A,B>::value> myclass
Где я ошибаюсь?
Вы забыли ключ класса:
template<class A, class B, bool isbaseof = std::is_base_of<A, B>::value>
struct myclass
// ^^^^^^
(Ключ класса может быть любым class
, struct
или union
.)
template<class A, class B> struct myclass<A,B,true>
.