我最初的方法是尝试模板化 Base 类。但是,据我确定,我不能使用非类型模板参数,即使在可以将字符串作为参数传递的 C++20 中(我相信在 C++20 方法中您只能传递一个字符串)。
当然,在 C++20 中,您可以将多个字符串作为非类型模板参数。
本质上,您可以做的是有一个名为fixed_string 的小包装类,它可以隐式地与const char* 相互转换。而且从 C++20 开始,可以将琐碎的结构体作为非类型模板参数,因此可以将这个轻量级包装类用作非类型模板参数。
这是一个 C++20 实现,可用于实现您想要做的事情:
#include <cstddef>
#include <type_traits>
// The wrapper class in question
template <std::size_t N>
struct fixed_string {
char str[N + 1] {};
constexpr fixed_string(const char* X) {
for (std::size_t i = 0; i < N; i++)
str[i] = X[i];
}
constexpr operator const char*() const {
return str;
}
};
template <std::size_t N>
fixed_string(const char (&)[N]) -> fixed_string<N - 1>;
// Stores a list of 'fixed_string's
template <fixed_string ...Strings>
struct fixed_string_list;
// Concatenates two 'fixed_string_list's together
template <typename, typename>
struct fixed_string_list_concat;
template <fixed_string ...Strings1, fixed_string ...Strings2>
struct fixed_string_list_concat<fixed_string_list<Strings1...>, fixed_string_list<Strings2...>> {
using type = fixed_string_list<Strings1..., Strings2...>;
};
// Fetches the string at the specified index in the 'fixed_string_list', the required string is put inside a 'fixed_string_list' and then returned back
template <typename, std::size_t>
struct fixed_string_list_get;
template <std::size_t I, fixed_string String1, fixed_string ...Strings>
struct fixed_string_list_get<fixed_string_list<String1, Strings...>, I> {
using type = typename fixed_string_list_get<fixed_string_list<Strings...>, I - 1>::type;
};
template <fixed_string String1, fixed_string ...Strings>
struct fixed_string_list_get<fixed_string_list<String1, Strings...>, 0> {
using type = fixed_string_list<String1>;
};
// Trims the list in the range [From, To)
template <typename, std::size_t, std::size_t>
struct fixed_string_list_trim;
template <std::size_t From, std::size_t To, fixed_string ...Strings>
requires (From < To)
struct fixed_string_list_trim<fixed_string_list<Strings...>, From, To> {
using type = typename fixed_string_list_concat<typename fixed_string_list_get<fixed_string_list<Strings...>, From>::type, typename fixed_string_list_trim<fixed_string_list<Strings...>, From + 1, To>::type>::type;
};
template <std::size_t From, std::size_t To, fixed_string ...Strings>
requires (From >= To)
struct fixed_string_list_trim<fixed_string_list<Strings...>, From, To> {
using type = fixed_string_list<>;
};
// Returns the 'fixed_string_list' excluding the string at the specified index
template <typename, std::size_t>
struct fixed_string_list_exclude;
template <std::size_t I, fixed_string ...Strings>
requires (I > 0 && I < sizeof...(Strings) - 1)
struct fixed_string_list_exclude<fixed_string_list<Strings...>, I> {
using type = typename fixed_string_list_concat<typename fixed_string_list_trim<fixed_string_list<Strings...>, 0, I>::type, typename fixed_string_list_trim<fixed_string_list<Strings...>, I + 1, sizeof...(Strings) - I + 1>::type>::type;
};
template <std::size_t I, fixed_string ...Strings>
requires (I == 0)
struct fixed_string_list_exclude<fixed_string_list<Strings...>, I> {
using type = typename fixed_string_list_trim<fixed_string_list<Strings...>, 1, sizeof...(Strings)>::type;
};
template <std::size_t I, fixed_string ...Strings>
requires (I == sizeof...(Strings) - 1)
struct fixed_string_list_exclude<fixed_string_list<Strings...>, I> {
using type = typename fixed_string_list_trim<fixed_string_list<Strings...>, 0, I>::type;
};
// Checks whether a 'fixed_string_list' contains a given string, the string to be found must also be within a 'fixed_string_list'
template <typename, typename>
struct fixed_string_list_contains;
template <fixed_string String, fixed_string ...Strings>
struct fixed_string_list_contains<fixed_string_list<Strings...>, fixed_string_list<String>> : std::bool_constant<((String == Strings) || ...)> {};
// Implementation detail for 'is_fixed_string_list_unique'
template <typename, std::size_t, std::size_t>
struct is_fixed_string_list_unique_impl;
template <std::size_t I, std::size_t Limit, fixed_string ...Strings>
struct is_fixed_string_list_unique_impl<fixed_string_list<Strings...>, I, Limit> : std::bool_constant<!fixed_string_list_contains<typename fixed_string_list_exclude<fixed_string_list<Strings...>, I>::type, typename fixed_string_list_get<fixed_string_list<Strings...>, I>::type>::value && is_fixed_string_list_unique_impl<fixed_string_list<Strings...>, I + 1, Limit>::value> {};
template <std::size_t I, fixed_string ...Strings>
struct is_fixed_string_list_unique_impl<fixed_string_list<Strings...>, I, I> : std::true_type {};
// Checks whether the given 'fixed_string_list' has no repeating strings inside
template <typename>
struct is_fixed_string_list_unique;
template <fixed_string ...Strings>
requires (sizeof...(Strings) > 1)
struct is_fixed_string_list_unique<fixed_string_list<Strings...>> : std::bool_constant<is_fixed_string_list_unique_impl<fixed_string_list<Strings...>, 0, sizeof...(Strings)>::value> {};
template <fixed_string ...Strings>
requires (sizeof...(Strings) <= 1)
struct is_fixed_string_list_unique<fixed_string_list<Strings...>> : std::true_type {};
现在你终于可以这样做了:
template <fixed_string ...Strings>
struct Base {
static_assert(is_fixed_string_list_unique<fixed_string_list<Strings...>>(), "Duplicate strings are not allowed!");
};
struct Derived_OK : public Base<"dog", "car", "time"> {};
// Results in a static assertion failure: "Duplicate strings are not allowed!"
struct Derived_BAD : public Base<"dog", "car", "time", "car"> {};
这里有一个链接,您可以自己尝试一下:
Demo