Расширение пакета параметров шаблона класса для конструкторов - PullRequest
3 голосов
/ 07 мая 2019

Я хотел бы создать шаблон класса RestrictedInteger, который может быть создан только с определенными значениями, известными во время компиляции.Вот как я мог бы сделать это вручную:

// Wrapper
template<int... Is> using IntList = std::integer_sequence<int, Is...>;

// This is my class
template<class intList> class RestrictedInteger;
template<int I1>
class RestrictedInteger<IntList<I1>> {
  const int _i;
public:
  constexpr RestrictedInteger(std::integral_constant<int, I1>) : _i(I1) {}
};
//[...]
template<int I1, I2, I3>
class RestrictedInteger<IntList<I1, I2, I3>> {
  const int _i;
public:
  constexpr RestrictedInteger(std::integral_constant<int, I1>) : _i(I1) {}
  constexpr RestrictedInteger(std::integral_constant<int, I2>) : _i(I2) {}
  constexpr RestrictedInteger(std::integral_constant<int, I3>) : _i(I3) {}
};
//[...] (and so on)

Естественно, я бы вместо этого хотел использовать шаблон с переменным числом аргументов. Если бы только это было законно :

template<int... Is>
class RestrictedInteger<IntList<Is...>> {
  int _i;
public:
  constexpr RestrictedInteger(std::integral_constant<int, Is>) : _i(Is) {}... // ERROR
}

Поскольку я использую C ++ 17, я думал, что это будет работать так:по-видимому, нет.

Есть идеи, как изящно решить эту проблему?

1 Ответ

4 голосов
/ 07 мая 2019

Если сбой компиляции возможен (вам не нужен компилятор для поиска других перегрузок) - вы можете поместить static_assert внутри вашего конструктора:

#include <type_traits>
#include <utility>

template<int... Is> using IntList = std::integer_sequence<int, Is...>;

template<class intList> class RestrictedInteger;

template<int... Is>
class RestrictedInteger<IntList<Is...>> {
private:
  const int _i;
public:
  template <int I>
  constexpr RestrictedInteger(std::integral_constant<int, I>) : _i(I) 
  {
      static_assert(((I == Is) || ...), "Invalid value");
  }
};

int main()
{
    RestrictedInteger<IntList<1, 2, 3>> i = std::integral_constant<int, 3>();
    RestrictedInteger<IntList<1, 2, 3>> ii = std::integral_constant<int, 6>(); // fails
}

или более подробное решение с std::enable_if:

#include <type_traits>
#include <utility>

template<int... Is> using IntList = std::integer_sequence<int, Is...>;

template<class intList> class RestrictedInteger;

template<int... Is>
class RestrictedInteger<IntList<Is...>> {
private:
  const int _i;
public:
  template <int I, typename std::enable_if_t<((I == Is) || ...)>* = nullptr>
  constexpr RestrictedInteger(std::integral_constant<int, I>) : _i(I) 
  {
  }
};

int main()
{
    RestrictedInteger<IntList<1, 2, 3>> i = std::integral_constant<int, 3>();
    RestrictedInteger<IntList<1, 2, 3>> ii = std::integral_constant<int, 6>(); // fails
}
...