C ++ variable argument of iteration pattern

I'm pretty inexperienced in such things, but I'm trying to create a template function that evaluates an n- variable function with the argument "rotated" (see the example below) and returns a vector of all these values.

For example, for n = 3 with the function f (x, y, z), the returned triple \ vector should be

< f (x, 0,0), f (0, x, 0), f (0,0, x)>

The naive version of what I need may look like this (it’s not necessary to \ work correctly)

typedef FunctionSignature Function; template<class Function, size_t Dimensions> std::array<Function::Out,Dimensions> F(Function::InComponent x) { std::array<Function::Out,Dimensions> Result; for (i=0; i<Dimensions; i++) Result[i] = Function::f("rotate((x,0,...,0),i)"); return Result; } 

But how to do a rotate thing.

I also hope that the for runtime can be eliminated in some way, since n well known at compile time.

+5
source share
1 answer
 template<class Function, size_t... Is, size_t... Js> typename Function::Out call_f(typename Function::InComponent x, std::index_sequence<Is...>, std::index_sequence<Js...>) { return Function::f((void(Is), 0)..., x, (void(Js), 0)...); } template<class Function, size_t Dimensions, size_t... Is> std::array<typename Function::Out, Dimensions> F(typename Function::InComponent x, std::index_sequence<Is...>) { return {{ call_f<Function>(x, std::make_index_sequence<Is>(), std::make_index_sequence<Dimensions - Is - 1>())... }}; } template<class Function, size_t Dimensions> std::array<typename Function::Out,Dimensions> F(typename Function::InComponent x) { return F<Function, Dimensions>(x, std::make_index_sequence<Dimensions>()); } 

For C ++ 11, do a SO search to implement make_index_sequence .

Demo

+5
source

All Articles