Is there any special syntax for initializing std :: array from another, different std :: array?

I have this situation:

class A {
    ...
};

class B {
    public:
        B(A x) { .... }
}

std::array<A, some_constant_value> init;
std::array<B, some_constant_value> arr = {
    init[0], 
    init[1],
    init[2],
    ...... ,
    init[some_constant_value-1]
};

Is there perhaps a better syntax than this to avoid putting all the elements down? (And it will not require intervention in the coincidence that some_constant_value will change?)

+5
source share
2 answers

I have this code. I think this is what you want:

  template<unsigned... Indices>
  struct indices {
    using next = indices<Indices..., sizeof...(Indices)>;
  };

  template<unsigned N>
  struct build_indices {
    using type = typename build_indices<N-1>::type::next;
  };
  template<>
  struct build_indices<0> {
    using type = indices<>;
  };

  namespace impl {
    template<typename To, typename From, unsigned... Is>
    std::array<To, sizeof...(Is)>
    array_convert_impl(std::array<From, sizeof...(Is)> const& from, indices<Is...>) {
      return std::array<To, sizeof...(Is)>{{ from[Is]... }}; 
    }
  } // namespace impl
  template<typename To, typename From, unsigned N>
  std::array<To, N>
  array_convert(std::array<From, N> const& from) {
    return impl::array_convert_impl<To>(from, typename build_indices<N>::type());
  }

Then you can do:

std::array<B, some_constant_value> arr = array_convert<B>(init);
+4
source

An alternative solution provided by the standard library is:

std::array<B, some_constant_value> 
arr((std::copy(init.begin(),init.end(),(&arr)->begin()),arr));

Note that the constructor argument is enclosed in ((...)), so that it is correctly parsed as a comma, and not as two arguments.

, B A. , , :

auto lamb = 
[&init]() -> B { static size_t i = 0; return B(init[i++]); };  
std::array<B, some_constant_value> 
arr((std::generate((&arr)->begin(),(&arr)->end(),lamb),arr));

, GCC 4.7.2, clang 3.2 Intel ++ 13.1.1, ( -g -O0 -Wall -std=c++11) :

#include <iostream>
#include <array>
#include <algorithm>

struct A 
{
    int _i = 42;
};

struct B 
{
    B(A x) 
    : _i(x._i){}
    int _i; 
};

struct C 
{
    explicit C(A x) 
    : _i(x._i){}
    int _i; 
};

using namespace std;

int main()
{
    array<A, 10> init;
    array<B, 10> arr((copy(init.begin(),init.end(),(&arr)->begin()),arr));
    cout << "arr contains..." << endl;
    for (size_t i = 0; i < arr.size(); ++i) {
        cout << arr[i]._i << endl;
    }
    auto lamb = 
    [&init]() -> C { static size_t i = 0; return C(init[i++]); };  
    array<C, 10> brr((generate((&brr)->begin(),(&brr)->end(),lamb),brr));
    cout << "brr contains..." << endl;
    for (size_t i = 0; i < brr.size(); ++i) {
        cout << brr[i]._i << endl;
    }
    return 0;
}
+1

All Articles