简体   繁体   中英

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, 10> 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?

Not with std::transform , but nothing a little template magic can't fix.

template<std::size_t N, std::size_t... Is>
std::array<Bar, N> foos_to_bars(const std::array<Foo, N>& foos,
                                std::index_sequence<Is...>) {
    return {{ Bar(foos[Is].m_1, foos[Is].m_2)... }};
}

template<std::size_t N, std::size_t... Is>
std::array<Bar, N> foos_to_bars(const std::array<Foo, N>& foos) {
    return foos_to_bars(foos, std::make_index_sequence<N>());
}

std::index_sequence and friends are C++14, but easily implementable in C++11. There are probably half a dozen implementations on SO alone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM