C ++ variational number of parameters of a variational pattern

Having a variational pattern is simple, and I can specialize it, so it only accepts TStringConstantwhich is string_constantfor some chars:

template <typename TStringConstant, typename TValue>
class entry;

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

If I wanted to create a template class that would take the variational number of TStringConstantdifferent chars, was there a way to do this? Perhaps with template template options?

So, all of the following conditions will 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'>>();

A bonus if it is rejected entry_list<something_else<'c','b','a'>>in the same way as it entry<something_else<'c','b','a'>, bool>will not be compiled.

+4
source share
2 answers

static_assert. , sfinae, , .

, :

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


}

are_string_constant :

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 {};

++ 17 ( are_string_constant):

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

, : entry_list?

bolov (+1), , .

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
 }

, static_assert(),

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

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


All Articles