jeudi 26 octobre 2017

substitutes for enable_if to enable a method for only one template value

I would like to make a template class limit an overloaded method like this example:

template<int N>
class foo {
  public:
    int add(int a) {return a + N;} 
    int add(std::enable_if<N == 0, int> a, int b) {return a + b}
}

However, I am using Visual Studio, whose support for SFINAE (and thus enable_if) is still not very good. (I'm not sure if it would work in that simple example; in my actual situation I'm using std::enable_if on a class reference and using a member of that class, and it doesn't work.) The answer to this (which has essentially the same error message I got) suggested using tag dispatching as an alternative, but I'm not sure how that would work if I want the function to simply be unavailable at all with those argument types, or if I want to overload a constructor in this manner.

Aucun commentaire:

Enregistrer un commentaire