在https://stackoverflow.com/a/1967183/134841中,提供了一种解决方案,用于静态检查成员是否存在,可能在某个类型的子类中:
template <typename Type>
class has_resize_method
{
class yes { char m;};
class no { yes m[2];};
struct BaseMixin
{
void resize(int){}
};
struct Base : public Type, public BaseMixin {};
template <typename T, T t> class Helper{};
template <typename U>
static no deduce(U*, Helper<void (BaseMixin::*)(), &U::foo>* = 0);
static yes deduce(...);
public:
static const bool result = sizeof(yes) == sizeof(deduce((Base*)(0)));
};
但是,它不适用于 C++11final
类,因为它继承自被测类,这会final
阻止。
OTOH,这个:
template <typename C>
struct has_reserve_method {
private:
struct No {};
struct Yes { No no[2]; };
template <typename T, typename I, void(T::*)(I) > struct sfinae {};
template <typename T> static No check( ... );
template <typename T> static Yes check( sfinae<T,int, &T::reserve> * );
template <typename T> static Yes check( sfinae<T,size_t,&T::reserve> * );
public:
static const bool value = sizeof( check<C>(0) ) == sizeof( Yes ) ;
};
无法在基类中找到该reserve(int/size_t)
方法。
是否有这个元函数的实现,它既可以在 的基类中找到reserved()
,并且如果是T
仍然可以工作?T
final