dimanche 23 août 2015

How to create a std::array with std::transform without default constructor

I have a std::array<Foo, 10> and I would like to create a std::array<Bar, 10> using a function from Foo to Bar. Ordinarily I would use std::transform like so:

array<Bar, 16> bars;
transform(foos.begin(), foos.end(), bars.begin(), [](Foo foo){
    return Bar(foo.m_1, foo.m_2);
});

However, Bar doesn't have a default constructor, so I can't create the bars array. I could always use vector but it would be nice to be able to use array to guarantee that that I always have exactly 10 elements. Is that possible?

Aucun commentaire:

Enregistrer un commentaire