vendredi 1 juillet 2016

How can I enable or disable inheritance from a template using another template parameter?


I want to enable a template to include functionality via CRTP and also use the same template without this functionality e.g. for classes that do not include a function requiered by the CRTP template.

The following code using std::conditional works:

#include <type_traits>

class NOCRTP{/*empty dummy class*/};
template <class T> class CRTP{/*addditional functionality here*/};

template<typename T, bool CRTP_ENABLED = true>
class Ok: public std::conditional<CRTP_ENABLED, CRTP<Ok<T> >, NOCRTP >::type{
};

The way without std::conditional via an additional template parameter does not since TYPE depends on itself.

template< typename T, typename TYPE> class Fail;
template< typename T, typename TYPE = CRTP< Fail<T,TYPE > > >
class Fail: public TYPE{
};

Can this be solved by some kind of forward declaration or is the std::conditional solution the only way ?


Aucun commentaire:

Enregistrer un commentaire