C++变长模板参数的变长数量

4
拥有可变参数模板很简单,我可以对其进行特化,使其仅接受一些charstring_constant类型的TStringConstant
template <typename TStringConstant, typename TValue>
class entry;

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

如果我想制作一个模板类,可以接受多个不同字符的TStringConstant,是否有方法可以做到这一点?也许可以使用模板模板参数?
因此以下所有内容都是有效的:
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'>>();

如果它能够像entry<something_else<'c','b','a'>, bool>一样拒绝entry_list<something_else<'c','b','a'>>,那就更好了。

2个回答

3
你可以使用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 {};

在C++17中,使用折叠表达式(因为不需要are_string_constant)实现更加容易:
template <class... Args>
struct entry {
    static_assert((... && is_string_constant<Args>::value),
                  "invalid template args for entry");
};

0
我看到的真正问题是:你想如何使用你的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...>), "!"); };

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接