dimanche 11 août 2019

Correct way to use std::enable_if

What are the differences between these classes? Precisely these methods with enable_if.

/// Alias of std::enable_if...
template <bool B, typename T = void>
using Enable_if = typename std::enable_if<B, T>::type;

Template<typename T, std::size_t N>
class A {
   ...
    template <std::size_t NN = N,
          typename = Enable_if<NN == 2>>
    Some_Return_Type
    method(param1, param2)
    {}

    template <std::size_t NN = N,
              typename = Enable_if<NN == 1>>
    Some_Return_Type
    method(param1)
    {}


};


Template<typename T, std::size_t N>
class B {
   ...
    Enable_if<N == 2, Some_Return_Type>
    method(param1, param2)
    {}

    Enable_if<N == 1, Some_Return_Type>
    method(param1)
    {}
};

What is the correct way to use enable_if in the case I have:

  • At least 2 methods that differs only in their arguments and they have the same name but just one of them must be "active" (if N == 1 one of them, if N == 2 the other).
  • Only one method that will be active if N == 0 and not active in other cases.

I would like to get something like:

Obj<int, 2> obj2;
Obj<int, 0> obj0;
Obj<int, 1> obj1;

if I write in the IDE obj0. it shows only methods of N == 0; obj1. only N == 1, ... .

Thanks.

Aucun commentaire:

Enregistrer un commentaire