Home > Net >  How to use 'std::enable_if_t' inside templated class, without introducing second template
How to use 'std::enable_if_t' inside templated class, without introducing second template

Time:09-15

I have a 100% working code, of Vector wrapper which does resizing differently for various types of vectors. However, when defining std::enable_if_t, I use

template <typename C = Vec> 

instead of using 'Vec' directly. Is there a way to reuse template argument 'Vec' of the class for its resizeListTo() method?

Full code:

template <class Vec>
class MyVectorWrapeer<Vec> {
 private:
  template <typename C = Vec>
  std::enable_if_t<someCondition<C, typename C::value_type>, void>
  static inline resizeListTo(C& c, uint32_t size) { /*Some Code*/ }
  template <typename C = Vec>
  std::enable_if_t<!someCondition<C, typename C::value_type>, void>
  static inline resizeListTo(C& c, uint32_t size) { /*Different Code*/ }

 public:
  static void deserializeVector(Vec* dst) {
    ...
    resizeListTo(*dst, size);
    ...
  }

I mean, something like

  template <>
  std::enable_if_t<someCondition<Vec, typename Vec::value_type>, void>
  static inline resizeListTo(Vec& c, uint32_t size) {

(The above code obviously does not compile)

CodePudding user response:

In C 20, you have requires to add constraint to the method:

template <class Vec>
class MyVectorWrapeer<Vec>
{
private:
  static void resizeListTo(Vec& c, uint32_t size) requires(someCondition<Vec>)
  { /*Some Code*/ }
  static void resizeListTo(Vec& c, uint32_t size) requires(!someCondition<Vec>)
  { /*Different Code*/ }
};

From your example, you want different implementations, so tag dispatch:

template <class Vec>
class MyVectorWrapeer<Vec>
{
private:
  static void resizeListTo_impl(Vec& c, uint32_t size, std::true_type) { /*Some Code*/ }
  static void resizeListTo_impl(Vec& c, uint32_t size, std::false_type) { /*Different Code*/ }

  static void resizeListTo(Vec& c, uint32_t size)
  {
    resizeListTo_impl(c, size, std::bool_constant<someCondition<Vec>>{});
  }
};

or since C 17, if constexpr (regular if might even do the job pre-c 17 if both branches are compilable):

template <class Vec>
class MyVectorWrapeer<Vec>
{
private:
  static void resizeListTo(Vec& c, uint32_t size)
  {
    if constexpr (someCondition<Vec>) {
       /*Some Code*/
    } else {
       /*Different Code*/
    }
  }
};
  • Related