fork download
  1. #include <iostream>
  2. #include <boost/array.hpp>
  3. #include <algorithm>
  4.  
  5. template <typename T, size_t N>
  6. inline boost::array<T,N>& to_array(T(&ar)[N]) {
  7. return *reinterpret_cast<boost::array<T,N>*>(&ar[0]);
  8. }
  9. template <typename T, size_t N>
  10. inline const boost::array<T,N>& to_array(const T(&ar)[N]) {
  11. return *reinterpret_cast<const boost::array<T,N>*>(&ar[0]);
  12. }
  13.  
  14. using namespace std;
  15.  
  16. int main(int ac, char* av[]) {
  17. const int hoge[] = {1,2,3,4,5};
  18. int foo[5];
  19. to_array(foo) = to_array(hoge);
  20. for (size_t i = 0; i < to_array(foo).size(); ++i) {
  21. cout << "foo[" << i << "]=" << foo[i] << endl;
  22. }
  23.  
  24. copy(to_array(hoge).rbegin(), to_array(hoge).rend(), to_array(foo).begin());
  25.  
  26. for (boost::array<int,5>::iterator i = to_array(foo).begin(); i != to_array(foo).end(); ++i) {
  27. cout << *i << endl;
  28. }
  29.  
  30. }
  31.  
Success #stdin #stdout 0.02s 2680KB
stdin
Standard input is empty
stdout
foo[0]=1
foo[1]=2
foo[2]=3
foo[3]=4
foo[4]=5
5
4
3
2
1