1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <array>
#include <type_traits>
#include <algorithm>
template < typename T, std::size_t N > struct my_array : public std::array<T,N>
{
template < typename U >
my_array< typename std::common_type<T,U>::type, N >
operator+ ( const std::array<U,N>& that ) const
{
my_array< typename std::common_type<T,U>::type, N > result ;
std::transform( this->begin(), this->end(), that.begin(), result.begin(),
[]( const T& aa, const U& bb ) { return aa + bb ; } ) ;
return result ;
}
my_array() = default ;
template < typename U >
my_array<T,N>( const std::array<U,N>& that )
{ std::copy( that.begin(), that.end(), this->begin() ) ; }
template < typename U >
my_array<T,N>& operator= ( const std::array<U,N>& that )
{
std::copy( that.begin(), that.end(), this->begin() ) ;
return *this ;
}
};
int main()
{
my_array<int,42> a ;
my_array<double,42> b ;
my_array<short,42> c = a + b;
}
|