Re: [eigen] comma initializers, perfect forwarding and variadic templates

[ Thread Index | Date Index | More Archives ]


this is interesting. API-wise, I'd rather go towards an alternative API to the current comma initializer syntax like:

mat.fill(expr1, expr2, expr3, ...);

that would internally be implemented as you described.

For your use case, one could directly forward std::forward<T>(args)... to such a fill() method. With this syntax no .finished() anymore:

foo( MatrixXd(10,10).fill(....) );

and we could also think about ways to automatically infer the matrix sizes by specifying the layout:


though I don't know to efficiently implement it with multiple memory reallocation/copies. Perhaps something like this would allows to defer the evaluations and assignments until we known the number of rows:

mat = assembleByRow(A11, A12, ....)
                  .(A21, ...)

This would construct a proxy holding references to the arguments that would then be processed by operator=.
It's just some random ideas that need to be refined!!


On Thu, Oct 20, 2016 at 4:20 PM, Massimiliano Janes <max.jns@xxxxxxxxx> wrote:
dear Eigen devs, first of all, thank you for this great library !

now, I have a feature request ( unless I missed an already existing way to do it, of course ).

I need to comma initialize some Eigen matrices by perfect-forwarding a parameter pack in a variadic template; clearly, this will be easily possible with c++17 fold expressions, but ,as of now, we have no ready made solution ( as far as I can tell ).

Yet, this can be easily implemented in c++11<= compilers < c++17, something equivalent to

template < typename H >
auto&& CommaUnPack( H&& head ){ return std::forward<H>(head); }


template < typename H, typename S, typename... T >
auto&& CommaUnPack( H&& head, S&& secnd, T&&... args )
    return CommaUnPack( std::forward<H>(head).operator,( std::forward<S>(secnd) ), std::forward<T>(args)... );

to be used as

CommaUnPack( SomeEigenExpr << std::forward<decltype(head)>(head), std::forward<decltype(tail)>(tail)... );

// or anything equivalent, like the possibly more consistent ( SomeEigenExpr << std::forward<decltype(head)>(head) ).finished_unpack( std::forward<decltype(tail)>(tail)... )
// or ...

note that, given the existing comma initializer implementation, this should never create dangling references and hence be relatively safe to use.
I know we can do it via library solutions offered by boost & others, but still, I think this would be a useful addition to Eigen initialization tools. What do you think ?

thank you, MJ

Mail converted by MHonArc 2.6.19+