动态值的Variadic Template类型



我正在编写某种源到源编译器,现在,我生成了一系列if语句,这些语句允许我实例化正确的模板对象。这些对象需要一个bool...,所以有些对象可能只需要一个,其他对象则需要多个。它看起来像:

if(unknow_at_compilation==1){
if(unknown2 == 3){
My_obj<true> A;
My_obj<true, false> B;
/*do something X*/
}else{
My_obj<true> A;
My_obj<false, false> B;
/*do same thing X*/
}else{
if(unknown2 == 3){
My_obj<false> A;
My_obj<true, true> B;
/*do same thing X*/
}else{
My_obj<false> A;
My_obj<false, true> B;
/*do same thing X*/
}
}

但是有更多的条件和对象。我不能使用多态性和指针,因为性能是我的应用程序中的一个关键点,并且对象AB被大量使用。

如果if,我想使用元编程,而不是复杂的继承。然而,我面临着一些严重的困难。。。在我这一代人中,我知道我需要的对象数量以及它需要多少"bool"。

第一步是将"做某事"部分封装在一个模板结构中,该模板结构采用布尔值集:

template<bool... val>
struct bool_set{};
template<typename T1, typename T2>
struct struct_do_something;
template<bool... b1, bool... b2>
struct struct_do_something<bool_set<b1...>, bool_set<b2...>>
{
static void do_something(){
My_obj<b1...> i;
My_obj<b2...> j;
/*Do something*/
}
};

这个部分有效,我可以这样称呼它(例如):struct_do_something<bool_set<true, true>, bool_set<false, false>>::do_something();

然后,我编写了一个使用条件生成单个bool_set的结构。

template<bool... static_vals> //bool values of the currently created bool_set
struct bool_set_generator{
template<typename... Bool>
static void select(bool v1, Bool... dynamic_vals) //Conditions in parameters
{
if(v1)
return bool_set_generator<static_vals..., true>::select(dynamic_vals...);
else
return bool_set_generator<static_vals..., false>::select(dynamic_vals...);
}
static void select(){
/*I have a bool_set here -> I can "do something"*/
struct_do_something<bool_set<static_vals...>>::do_something();
}
};

很明显,它还没有结束:我可以生成一个bool_set,所以我的想法是存储已经创建的bool_set+当前创建的那个:

template <typename... Created, bool... static_val>

但编译器(g++5.4,-std=c++11)告诉我parameter pack ‘Created’ must be at the end of the template parameter list,我把它和另一个切换,它说的是一样的。。。

有人有主意吗?最后,我想这样调用我的函数(或等效函数):

generate_the_do_something<>::call({cond1, cond2}, {cond3, cond4, cond5});

每个初始化器列表都是一个bool_set,而这个generate_the_do_something是一个(函数?的)结构,它将创建bool_set并用它们调用do_something()

如果您接受用特定的分隔符分隔bool的集合,如以下示例中所示

call_do_something(false, true, end_set{}, false, false, true, end_set{});

使用CCD_ 16(封装完成的CCD_ 17)相对容易。

请参阅以下工作(至少…编译)示例

#include <tuple>
template<bool...>
struct bool_set{};
struct end_set{};
template <bool...>
struct My_obj{};
template <typename, typename>
struct do_something;
template <bool... bs1, bool... bs2>
struct do_something<bool_set<bs1...>, bool_set<bs2...>>
{
static void func ()
{
My_obj<bs1...> mo1;
My_obj<bs2...> mo2;
(void)mo1; // just to avoid a lot warnings
(void)mo2; // just to avoid a lot warnings
// do something else
}
};
template <typename, typename>
struct gtds; // ex generate_the_do_something
template <typename ... Ts, bool ... Bs>
struct gtds<std::tuple<Ts...>, bool_set<Bs...>>
{
template <typename ... As>
static void call (bool const & val, As const & ... as)
{
if ( val )
gtds<std::tuple<Ts...>, bool_set<Bs..., true>>::call(as...);
else
gtds<std::tuple<Ts...>, bool_set<Bs..., false>>::call(as...);
}
template <typename ... As>
static void call (end_set const &, As const & ... as)
{ gtds<std::tuple<Ts..., bool_set<Bs...>>, bool_set<>>::call(as...); }
template <bool bsEmpty = (sizeof...(Bs) == 0U)>
static typename std::enable_if< ! bsEmpty >::type call ()
{ do_something<Ts..., bool_set<Bs...>>::func(); }
template <bool bsEmpty = (sizeof...(Bs) == 0U)>
static typename std::enable_if< bsEmpty >::type call ()
{ do_something<Ts...>::func(); }
};
template <typename ... Ts>
void call_do_something (Ts const & ... ts)
{ gtds<std::tuple<>, bool_set<>>::call(ts...); }

int main ()
{
call_do_something(false, true, end_set{}, false, false, true);
call_do_something(false, true, end_set{}, false, false, true, end_set{});
}

观察SFINAE的使用(在没有参数的call()上的std::enable_if),以允许在end_set{}结束或不结束的情况下调用call_do_something()

最新更新