Pie*_*ave 3 c++ templates metaprogramming c++03
我试图想办法循环一个模板参数列表,但没有成功
我不能使用c ++ 11可变参数模板功能,它需要在编译时完成
我可以假设在否定之后将没有积极的论据
任何的想法 ?
template< int F1, int F2 ,int F3>
struct TemplatedClass
{
TemplatedClass();
update()
{
/*
for each positive template argument
call a method
*/
}
};
Run Code Online (Sandbox Code Playgroud)
if您可以将所有参数放入数组中并迭代遍历,而不是编写一系列语句.这种方式编译器将无法优化您的代码(您没有指定是否需要这样做),但我认为它看起来会更清晰.例如
template<int F1, int F2 ,int F3>
struct TemplatedClass
{
TemplatedClass();
update()
{
const int array[] = {F1, F2, F3};
// replace this with std::for_each(...) with functors you need
for (int i = 0; i < sizeof(array)/sizeof(array[0]); ++i)
{
myfunc(array[i]);
}
}
}
Run Code Online (Sandbox Code Playgroud)