mardi 27 août 2019

How to use std::enable_if on method of templated class with seperate declaration and definition via specialization

I'm trying to have a templated class split between a header file and implementation using specialization, but I want one method to only appear in some specializations.

Header file:

template <typename T>
class A
{
  public:
  void foo();
  void bar();

  template<typename U = T, typename std::enable_if<std::is_convertible<int,U>::value>>::type*=nullptr>
  void special();
};

The implementation:

template<typename T>
void A<T>::foo()
{
  ...
}

template<typename T>
void A<T>::bar()
{
  ...
}

template<typename T, typename std::enable_if<std::is_convertible<int,T>::value>::type>
void A<T>::special()
{
  ...
}

// generate specializations
template
class A<float>;

template
class A<int>;

template
class A<std::string>;

However I keep getting error: declaration is incompatible with function template "void A<T>::special()" when I try it like this, or when I move the std::enable_if to be the return type. How should the definition be to match the declaration of this method special()?

Aucun commentaire:

Enregistrer un commentaire