mercredi 28 novembre 2018

Different member function definition according to compile-time condition

As per this answer, I've been using

 template <typename T,  typename = typename enable_if<bool_verfier<T>()>::type> > classMember(const T& arg);

As the function signature for several class members, where bool_verifier<T>() is a templated function that asserts that a particular class T fulfills certain requirements, with return type constexpr bool. This ensures a particular overload of classMember(const T& arg) is only used for particular argument types, but it is not possible to do this when there are multiple overloads with the same prototype/argument signature, because the compiler won't allow it:

// ...
template <typename T,  typename = typename enable_if<bool_verfier<T>()>::type> > classMember(const T& arg);
template <typename T,  typename = typename enable_if<!(bool_verfier<T>())>::type> > classMember(const T& arg);
// ...

which causes the following compilation error:

 ‘template<class T, class> void myClass::classMember<T>(const T&)’ cannot be overloaded with ‘template<class T, class> void std::myClass<T>::classMember(const T&)’

If I need classMember to have different definitions according to whether or not bool_verifier<T>() returns true, what would be the correct syntax/member declaration? Alternatively, is there a way to call bool_verifier<T> from an #if precompiler conditional statement?

Aucun commentaire:

Enregistrer un commentaire