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
か?