Reputation: 1272
I have a vector class template <unsigned int L> class Vec
of variable coordinate count L
.
I would like to implement the field selection feature of glsl which allows you to create new vectors by selecting like vec4 a=vec4(1,2,3,4); vec4 b=a.xyxz; //b is (1,2,1,3).
In my program i would like create something like that:
Vec<3> a={7,8,9};
Vec<4> b=a.select(0,2,2,1); //each argument is an index of the coordinate to use.
Vec<5> c=b.select(0,1,2,3,1);
Solution:
template<typename... Args,unsigned int S=sizeof...(Args)> Vec<S> select(Args&&... args){
Vec<S> result;
int indices[S]={args...};
for(int i=0;i<S;i++){
result[i]=this->v[indices[i]]; //v is the float array that stores the values.
}
return result;
}
and some ridiculous examples to see if it works:
Vec<3> a={7,8,9};
Vec<9> b=a.select(0,0,1,1,0,0,1,1,2);
Vec<1> c=a.select(2);
a=[7,8,9]
b=[7,7,8,8,7,7,8,8,9]
c=[9]
Upvotes: 2
Views: 511
Reputation: 55425
Like this:
template<int N>
class Vec {};
template<typename... Args>
auto foo(Args&&...) -> Vec<sizeof...(Args)>;
int main()
{
auto v = foo(1,2,3);
Vec<1> vv = foo(5);
}
It works with the old-style function signature syntax, too (I just prefer trailing return type in this particular case).
Upvotes: 3