Std :: copy for multidimensional arrays

I tried the other day with gcc-4.9.1:

int main()
{
  int a[10][20][30];
  int b[10][20][30];

  ::std::copy(::std::begin(a), ::std::end(a), ::std::begin(b));

  return 0;
}

and of course he made an error:

In file included from /usr/include/c++/4.9.2/bits/char_traits.h:39:0,
                 from /usr/include/c++/4.9.2/ios:40,
                 from /usr/include/c++/4.9.2/ostream:38,
                 from /usr/include/c++/4.9.2/iostream:39,
                 from t.cpp:1:
/usr/include/c++/4.9.2/bits/stl_algobase.h: In instantiation of 'static _Tp* std::__copy_move<_IsMove, true, std::random_access_iterator_tag>::__copy_m(const _Tp*, const _Tp*, _Tp*) [with _Tp = int [20][30]; bool _IsMove = false]':
/usr/include/c++/4.9.2/bits/stl_algobase.h:396:70:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = int (*)[20][30]; _OI = int (*)[20][30]]'
/usr/include/c++/4.9.2/bits/stl_algobase.h:434:38:   required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = int (*)[20][30]; _OI = int (*)[20][30]]'
/usr/include/c++/4.9.2/bits/stl_algobase.h:466:17:   required from '_OI std::copy(_II, _II, _OI) [with _II = int (*)[20][30]; _OI = int (*)[20][30]]'
t.cpp:10:62:   required from here
/usr/include/c++/4.9.2/bits/stl_algobase.h:373:4: error: static assertion failed: type is not assignable
    static_assert( is_copy_assignable<_Tp>::value,
    ^

I think this is not so much a problem with std::copy()as with std::begin()and std::end()that will handle multidimensional arrays. Is this an omission with the current C ++ standard and how to get around it?

EDIT: I am convinced that the standard can solve this problem:

namespace std
{

template <typename T, size_t M, size_t N>
constexpr typename remove_all_extents<T>::type*
begin(T (&array)[M][N])
{
  return begin(array[0]);
}

template <typename T, size_t M, size_t N>
constexpr typename remove_all_extents<T>::type*
end(T (&array)[M][N])
{
  return end(array[M - 1]);
}

}
+4
source share
6 answers

It will not work with a simple call copy, since simple arrays cannot be copied or assigned. However, you can copy the basic elements and treat the multidimensional array as one-dimensional.

:

// For convenient trailing-return-types in C++11:
#define AUTO_RETURN(...) \
 noexcept(noexcept(__VA_ARGS__)) -> decltype(__VA_ARGS__) {return (__VA_ARGS__);}

template <typename T>
constexpr auto decayed_begin(T&& c)
AUTO_RETURN(std::begin(std::forward<T>(c)))

template <typename T>
constexpr auto decayed_end(T&& c)
AUTO_RETURN(std::end(std::forward<T>(c)))

template <typename T, std::size_t N>
constexpr auto decayed_begin(T(&c)[N])
AUTO_RETURN(reinterpret_cast<typename std::remove_all_extents<T>::type*>(c    ))

template <typename T, std::size_t N>
constexpr auto decayed_end(T(&c)[N])
AUTO_RETURN(reinterpret_cast<typename std::remove_all_extents<T>::type*>(c + N))

std::copy( decayed_begin(a), decayed_end(a), decayed_begin(b) );

.

+4

. int.

,

std::copy( reinterpret_cast<int *>( a ),
           reinterpret_cast<int *>( a ) + 10 * 20 * 30,
           reinterpret_cast<int *>( b ) );

std::for_each std::transform -.

+4

, 5 , , , :

template <typename T>
constexpr T* begin(T& value) noexcept
{
  return &value;
}

template <typename T, ::std::size_t N>
constexpr typename ::std::remove_all_extents<T>::type*
begin(T (&array)[N]) noexcept
{
  return begin(array[0]);
}

template <typename T>
constexpr T* end(T& value) noexcept
{
  return &value + 1;
}

template <typename T, ::std::size_t N>
constexpr typename ::std::remove_all_extents<T>::type*
end(T (&array)[N]) noexcept
{
  return end(array[N - 1]);
}

, - , , .

+2

, , . , , :

template <typename T>
typename std::remove_all_extents<T>::type* mbegin(T& arr) {
    return reinterpret_cast<typename std::remove_all_extents<T>::type*>(&arr);
}
template <typename T>
size_t msize(const T& a) 
{
    return sizeof(T) / sizeof(typename std::remove_all_extents<T>::type);
}
template <typename T>
typename std::remove_all_extents<T>::type* mend(T& arr) {
    return reinterpret_cast<typename std::remove_all_extents<T>::type*>(&arr)+msize(arr);
}

:

::std::copy(mbegin(a), mend(a), mbegin(b));

T (int[][]..[]) typename std::remove_all_extents<T>::type (int).

+2

. , ++ , , ( ) , .

. :

template <typename T, size_t N1, size_t N2, size_t N3>
T* begin(T(&arr)[N1][N2][N3]) {
    return reinterpret_cast<T*>(arr);  
}
template <typename T, size_t N1, size_t N2, size_t N3>
T* end (T(&arr)[N1][N2][N3]) {
    return reinterpret_cast<T*>(arr)+N1*N2*N3; 

:

::std::copy(begin(a), end(a), begin(b));

3D .

+1

How about using a union with a flattened version.

int main()
{
  union
  {
    int a[10][20][30];
    int fa[10 * 20 * 30];
  };

  union
  {
    int b[10][20][30];
    int fb[10 * 20 * 30];
  };

  ::std::copy(::std::begin(fa), ::std::end(fa), ::std::begin(fb));

  return 0;
}
+1
source

All Articles