我正在尝试创建一个is_foo
函数,然后我可以使用 withenable_if
来确定类型是否派生自某个 CRTP 基类。下面的代码是我实现该功能的尝试is_foo
,但它实际上不起作用。有人能告诉我我需要改变什么来修复它吗?
谢谢。
#include <iostream>
#include <type_traits>
#include <functional>
using namespace std;
template <class Underlying, class Extra>
struct Foo
{
int foo() const { return static_cast<const Underlying*>(this)->foo(); }
};
template<class T>
struct Bar : Foo<Bar<T>, T>
{
int foo() const { return 42; }
};
template<class T>
struct is_foo { static const bool value = false; };
template<class Underlying, class Extra>
struct is_foo<Foo<Underlying, Extra> > { static const bool value = true; };
template<class T>
void test(const T &t)
{
cout << boolalpha << is_foo<T>::value << endl;
}
int main()
{
Bar<int> b;
test(b);
}