Class template parameter pack expansion for constructors

AlwaysMoving

I'd like to make a class template RestrictedInteger that can only be constructed with certain values known at compile time. This is how I could do it manually:

// 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)

Naturally, I'd like to use a variadic template instead. If only this were legal:

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

Since I'm using C++17 however, I thought it would work like this:

template<int... Is>
class RestrictedInteger<IntList<Is...>> {
  int _i;
public:
  template<int I>
  constexpr RestrictedInteger(std::enable_if_t<...||(I==Is), std::integral_constant<int, I>>) : _i(I) {} // syntax error: '...' (Visual Stuio 2019)
};

But apparently not.

Any ideas of a neat way to solve this?

Dmitry Gordon

If failing compilation is an option (you don't need compiler to find other overloads) - you can put static_assert inside your constructor:

#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
}

or a bit more verbose solution with 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
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Variadic template pack expansion

Pack expansion for alias template

when template parameter of a template template-parameter is pack expansion, gcc fails, clang succeeds

Clang fails to compile parameter pack expansion using template metaprogramming

Order of parameter pack expansion

Simultaneous parameter pack expansion error for unused template type definition

Having trouble with parameter pack expansion

Nested parameter pack expansion

How to "duplicate" template parameter pack expansion?

template Parameter pack expansion on function call with array of data

Store template parameter pack as a attribute of a non class template

Is a parameter pack a template parameter?

Referring to template parameter pack in class

g++ and clang++ are both bugged with template function parameter pack expansion?

Nested parameter pack expansion fails

Divorce a parameter pack in a class template

double template parameter pack

Variadic template indexed pack expansion

a variadic template as a template parameter without pack expansion

Parameter pack expansion questions

parameter pack expansion not working in lambda

C++ template variable and parameter pack expansion

Mixin constructors nested pack expansion

Template pack expansion with functions

Parameter pack expansion for variadic class member (tuple or other)

non type template parameter pack expansion

Variadic template parameter pack expansion looses qualifier

How to explicitly tell compiler to choose exactly one parameter template during template pack expansion?

Variadic template variable parameter pack expansion in recursive variable definition has incorrect size

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive