Variadic pattern for both types of names and scalar constants

I am trying to implement a generic class template that passes template arguments to another class, for example:

template <typename... Args>
class A : public B<Args...>
{
    ...

The problem is that B can have a scalar constant pattern as follows:

template <size_t N>
class B
{
    ...

or even so:

template <typename T, size_t N>
class B
{
    ...

So, is there a way to accept mixed types of names and scalar constants as parameters of a variational pattern?

+4
source share
1 answer

You can embed values ​​in types. This is a method that boost has been using for a long time, if I correctly reminded it, and was added to the standard library in C ++ 11 as std::integral_constant(which indicates its usefulness).

With C ++ 17, everything has become even simpler.

template<auto val>
struct constant : std::integral_constant<decltype(val), val> {};

B2 B

template<typename T, typename N>
struct B2 { using type = B<T, N::value>; };

template<typename... Args>
struct A : public B2<Args...>::type {};

A<int, constant<42>> a;
+2

Source: https://habr.com/ru/post/1690108/


All Articles