typedef enum colors {
MIN_COLOR,
RED = MIN_COLOR,
GREEN,
MAX_COLOR
} colors;
template< colors C >
struct MapEntry {
std::string s;
MapEntry(std::string s_):s(s_) {}
};
void do_in_order() {}
template<typename F0, typename... Fs>
void do_in_order(F0&& f0, Fs&&... fs) {
std::forward<F0>(f0)();
do_in_order( std::forward<Fs>(fs)... );
}
struct MapInit {
std::map< std::string, color > retval;
operator std::map< std::string, color >() {
return std::move(retval);
}
template<colors C>
void AddToMap( MapEntry<C>&& ent ) {
retval.insert( std::make_pair( std::move(end.s), C ) );
}
template< typename... Entries >
MapInit( Entries&& entries ) {
do_in_order([&](){ AddToMap(entries); }...);
}
};
template<typename... Entries>
MapInit mapInit( Entries&&... entries ) {
return MapInit( std::forward<Entries>(entries)... );
}
const std::map<std::string, colors> = mapInit( MapEntry<RED>("red"), MapEntry<GREEN>("green") );
它为您提供了一种 C++11 方法,可以从编译时 color 和运行时 string 数据构造 std::map。
接下来将“MapEntry<colors> 列表添加到colors 列表”元函数中。
template<colors... Cs>
struct color_list {};
template<typename... Ts>
struct type_list {};
template<typename MapEnt>
struct extract_color;
template<colors C>
struct extract_color<MapEntry<C>> {
enum {value=C};
};
template<typename Entries>
struct extract_colors;
template<typename... MapEntries>
struct extract_colors<type_list<MapEntries...>> {
typedef color_list< ( (colors)extract_colors<MapEntries>::value)... > type;
};
对该列表进行排序。检测重复 - 如果有,你搞砸了。
编译时排序比其余部分更难,而且要超过 100 行以上的代码。如果你不介意的话,我会忽略它! Here is a compile time merge sort I wrote in the past to answer a stack overflow question 可以通过相对简单的适配工作(它使用值对类型进行排序,在这种情况下,我们直接对编译时值列表进行排序)。
// takes a sorted list of type L<T...>, returns true if there are adjacent equal
// elements:
template<typename clist, typename=void>
struct any_duplicates:std::false_type {};
template<typename T, template<T...>class L, T t0, T t1, T... ts>
struct any_duplicates< L<t0, t1, ts...>, typename std::enable_if<t0==t1>::type>:
std::true_type {};
template<typename T, template<T...>class L, T t0, T t1, T... ts>
struct any_duplicates< L<t0, t1, ts...>, typename std::enable_if<t0!=t1>::type>:
any_duplicates< L<t1, ts...> > {};
检测colors 有效范围之外的元素(即<MIN_COLOR 或>=MAX_COLOR)。如果是这样,你搞砸了。
template<typename List>
struct min_max;
template<typename T, template<T...>class L, T t0>
struct min_max {
enum {
min = t0,
max = t1,
};
};
template<typename T, template<T...>class L, T t0, T t1, T... ts>
struct min_max {
typedef min_max<L<t1, ts...>> rest_of_list;
enum {
rest_min = rest_of_list::min,
rest_max = rest_of_list::max,
min = (rest_min < t0):rest_min:t0,
max = (rest_max > t0):rest_max:t0,
};
};
template< typename T, T min, T max, typename List >
struct bounded: std::integral_constant< bool,
(min_max<List>::min >= min) && (min_max<List>::max < max)
> {};
计算有多少元素——应该有MAX_COLOR 元素。如果没有,你搞砸了。
template<typename List>
struct element_count;
template<typename T, template<T...>L, T... ts>
struct element_count<L<ts...>>:std::integral_constant< std::size_t, sizeof...(ts) > {};
如果这些都没有发生,那么您一定已经初始化了它们中的每一个。
唯一缺少的是您可以离开并为两个值使用相同的string。由于编译时strings 很麻烦,只需在运行时检查它(map 中的条目数等于初始化后colors 的数量)。
在 C++03 中这样做会更难。您缺少可变参数模板,因此您最终不得不伪造它们。这是一种痛苦。 mpl 或许能帮到你。
2012 年 11 月的 MSVC CTP 编译器更新中提供了可变参数模板。
这是一个没有重复检查和没有边界检查的玩具示例(它只是检查地图条目的数量是否匹配);
#include <cstddef>
#include <utility>
#include <string>
#include <map>
enum TestEnum {
BeginVal = 0,
One = BeginVal,
Two,
Three,
EndVal
};
template<TestEnum e>
struct MapEntry {
enum { val = e };
std::string s;
MapEntry( std::string s_ ):s(s_) {}
};
void do_in_order() {}
template<typename F0, typename... Fs>
void do_in_order(F0&& f0, Fs&&... fs) {
std::forward<F0>(f0)();
do_in_order( std::forward<Fs>(fs)... );
}
template<typename... MapEntries>
struct count_entries:std::integral_constant< std::size_t, sizeof...(MapEntries) > {};
// should also detect duplicates and check the range of the values:
template<typename... MapEntries>
struct caught_them_all:
std::integral_constant<
bool,
count_entries<MapEntries...>::value == (TestEnum::EndVal-TestEnum::BeginVal)
>
{};
struct BuildMap {
typedef std::map<std::string, TestEnum> result_map;
mutable result_map val;
operator result_map() const {
return std::move(val);
}
template<typename... MapEntries>
BuildMap( MapEntries&&... entries ) {
static_assert( caught_them_all<MapEntries...>::value, "Missing enum value" );
bool _[] = { ( (val[ entries.s ] = TestEnum(MapEntries::val)), false )... };
}
};
std::map< std::string, TestEnum > bob = BuildMap(
MapEntry<One>("One")
,MapEntry<Two>("Two")
#if 0
,MapEntry<Three>("Three")
#endif
);
int main() {}
将#if 0 替换为#if 1 以观察它的编译。 Live link如果你想玩。