lundi 31 août 2015

Generate a std::tuple from standard container

Is there a portable way to generate a std::tuple (really a std::array) from the contents of a container? Such a tuple would allow std::apply to extract function arguments from a container.

My first attempt, using tail recursion, fails with compiler error: "recursive template instantiation exceeds maximum...".

I could not quite get my second attempt (std::for_each with a mutable lambda holding tuple) to compile with desired results.

I assume that something along the lines of how boost::mpl handles variadic metafunctions (ie magic using boost::preprocessor) could be made to work -- but that's so c++03. I'm hoping there's a better solution.

The function signature would look something like:

std::list<int> args_as_list = {1, 2, 3, 4};
auto tpl = args_as_tuple(args_as_list);

where type of tpl is std::array<int const, 4>.

Thanks for suggestions!

Aucun commentaire:

Enregistrer un commentaire