Bernard
Bernard

Reputation: 5690

C++ variadic number of variadic template parameters

Having a variadic template is simple, and I can specialize it so it will only accept a TStringConstant that is a string_constant of some chars:

template <typename TStringConstant, typename TValue>
class entry;

template <char... key, typename TValue>
class entry<string_constant<key...>, TValue>{}

If I wanted to make a template class that would accept a variadic number of TStringConstants of different chars, would there be a way to do it? Perhaps with template template parameters?

So that all of the following would be valid:

entry_list<string_constant<'c','b','a'>, string_constant<'d','e','f','g'>>();
entry_list<string_constant<'c','b','a'>, string_constant<'d','e','f','g'>, string_constant<'d','e','z','z'>>();
entry_list<string_constant<'a','b','c'>>();

Bonus if it will reject entry_list<something_else<'c','b','a'>> just like entry<something_else<'c','b','a'>, bool> will be fail to compile.

Upvotes: 4

Views: 274

Answers (2)

max66
max66

Reputation: 66230

The real problem I see is: how do you want to use the variadic lists of chars of your entry_list class?

I like the bolov's solution (+1) but, if you accept a recursive solution, I propose the use of inheritance.

The following is a full example

template <char ...>
struct string_constant
 { };

template <char ...>
struct something_else
 { };

template <typename ...>
class entry_list;

template <>
class entry_list<>
 { };

template <char ... keys, typename ... Scs>
class entry_list<string_constant<keys ...>, Scs ...>
   : public entry_list<Scs ...>
 { };

int main ()
 {
   entry_list<string_constant<'c','b','a'>,
              string_constant<'d','e','f','g'>>(); // compile

   entry_list<string_constant<'c','b','a'>,
              string_constant<'d','e','f','g'>,
              string_constant<'d','e','z','z'>>(); // compile

   entry_list<string_constant<'a','b','c'>>(); // compile

   //entry_list<something_else<'c','b','a'>>(); // compilation error

   //entry_list<string_constant<'c','b','a'>, bool>(); // compilation error
 }

If you don't want use inheritance, you can use static_assert() instead as follows

template <char ... keys, typename ... Scs>
class entry_list<string_constant<keys ...>, Scs ...>
 { static_assert(sizeof(entry_list<Scs...>), "!"); };

Upvotes: 0

bolov
bolov

Reputation: 75874

You can do it with a static_assert. I don't know how to implement it in a sfinae friendly way, but I guess you don't care about that.

So here it goes:

template <class... Args> struct entry {
    static_assert(are_string_constant<Args...>::value, "invalid template args for entry");
};

auto test()
{
  entry<string_constant<'c', 'd'>> e1; // OK
  entry<string_constant<'c', 'd'>, string_constant<'a', 'b', 'c', 'd'>> e2; // OK

  // entry<int,
  //       string_constant<'c', 'd'>,
  //       string_constant<'a', 'b', 'c', 'd'>> e3; // static_assert kicks in

  // entry<definitely_not_string_constant<'c', 'd'>,
  //       string_constant<'a', 'b', 'c', 'd'>> e4; // static_assert kicks in


}

The building of are_string_constant is pretty straight forward:

template <char... Args> struct string_constant {};
template <char... Args> struct definitely_not_string_constant {};

// --- is_string_constant -----

template <class T> struct is_string_constant : std::false_type {};

template <char... Args>
struct is_string_constant<string_constant<Args...>> : std::true_type {};

// --- are_string_constant -----    

template <class... Args> struct are_string_constant;

template <class A0, class... Args>
struct are_string_constant<A0, Args...>
     : std::integral_constant<bool, (is_string_constant<A0>::value &&
                                     are_string_constant<Args...>::value)>::type
{};

template <class T> struct are_string_constant<T> : is_string_constant<T>::type {};

In c++17 the implementation is easier with fold expressions (because you don't need the are_string_constant):

template <class... Args>
struct entry {
    static_assert((... && is_string_constant<Args>::value),
                  "invalid template args for entry");
};

Upvotes: 3

Related Questions