Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: parameter pack expansion inside lambda fails

I'm trying to create a way to directly expand multiple parameter packs. I have created a function template<size_t X,typename F> auto sequenceFunc(F&& f), that calls a given function f with a expanded integer_sequence.

This works well for small functions like this:

template<typename T,
         size_t A,size_t B>
vec<B,T> col(const mat<A,B,T>& a,const size_t& i){
    return sequenceFunc<A>([&](auto... J) -> vec<B,T>{
        return { a[J][i]... }; //expands to a[0][i], a[1][i], ... a[A-1][i]
    });
}

Unfortunately I can't expand multiple parameter packs, even if I follow the rule, that only one parameter pack can be inside a ...-expression. This is my attempt at using this function for matrix multiplication:

template<typename S,typename T,
         size_t A,size_t B,size_t C>
mat<C,B,S> mul(const mat<A,B,S>& a,const mat<C,A,T>& b){
    return sequenceFunc<B>([&](auto... I)->mat<C,B,S>{ //for all B rows in a...
        return {
            sequenceFunc<C>([&](auto... J)->vec<C,S>{ // ... look at all C columns in b and calculate dot product.
                auto i = I; //putting "I" outside the expansion of "J"
                return {
                    dot(row(a,i),col(b,J))... //expands J
                };
            })... //expands I
        };
    });
}

This is the error:

error: parameter packs not expanded with '...':
      auto i = I;
               ^

I don't really understand why an expansion is necessary, because there is another ... outside the expression. I use GCC 5.1.0.

Information vec and mat are only using-declarations for std::array and a nested std::array<std::array<A,T>,B>

like image 525
tly Avatar asked Sep 27 '22 20:09

tly


1 Answers

This is gcc bug 47226. It's still open, and the code example still fails on gcc 5.2.0, while it compiles just fine on clang 3.6. Your code looks correct to me.

like image 135
Barry Avatar answered Nov 15 '22 08:11

Barry