Home > Mobile >  Reduce number of template parameters
Reduce number of template parameters

Time:10-29

I would like to store some objects I know at compile-time in a class, and keep them constexpr, in order to proceed at compile-time. However, the way I'm storing these values in a struct seems unsatisfactory:

template <class T1, T1 _x1, class T2, T2 _x2>
struct A
{
   constexpr static T1 x1 = _x1;
   constexpr static T1 x2 = _x2;
}

While the code above achieves my goal, it seems unnecessarily complicated to have to provide both type and value explicitly in order to store a constexpr value in a templated class.

Is there a better/more elegant way of achieving this? In particular one, where I do not have to deduce the type again first would be desirable.

CodePudding user response:

In C 17 you can have the auto template parameters

template <auto _x1, auto _x2>
struct A
{
   // Use _x1 and _x2 directly
}
  • Related