mercredi 31 décembre 2014

How to get a SFINAE expression to work with template and non-template classes?

I have made this for SFINAE:



// Type 'type' exists iff X is a base of COLLECTION
template<typename X, typename COLLECTION, typename RET_TYPE = void>
struct enable_if_is_base_of : std::enable_if<std::is_base_of<X, COLLECTION>::value, RET_TYPE>
{};


Which works well for something like this:



class A {}; class B {}; class C{};
class collection : A, B {};

template <typename X>
typename enable_if_is_base_of<X, collection>::type fn(X&& x) { }

int main() {

fn(A());
fn(B());
// fn(C()); // Fails as expected
return 0;
}


However, if I want collection to be derived from a template as well as non-template classes, I'm not sure how I would go about that.


Something like this:



class A {};
template <typename X> class B {};
class C {};
template <typename X> class D {};

template <typename X>
class collection : A, B<X> {};

Aucun commentaire:

Enregistrer un commentaire