Reputation: 2583
I'm trying to implement a generic class template which forwards it's template arguments to another class, like this:
template <typename... Args>
class A : public B<Args...>
{
...
The problem is what B can have scalar constant template like this:
template <size_t N>
class B
{
...
or even like this:
template <typename T, size_t N>
class B
{
...
So, is there any way to accept mixed up typenames and scalar constants as a variadic template parameters?
Upvotes: 4
Views: 109
Reputation: 21130
You can embed values into types. This is a technique long used by boost if I recalled correctly, and was added to the standard library in C++11 as std::integral_constant
(indicative of its usefulness).
With C++17, things got even simpler.
template<auto val>
struct constant : std::integral_constant<decltype(val), val> {};
In conjunction with an alias B2
for B
, you use it as
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;
Upvotes: 2