Boost logo

Boost Users :

Subject: [Boost-users] [multi_array] std::swap and subarray
From: Sergey Mitsyn (svm_at_[hidden])
Date: 2010-02-17 04:53:54


Hello!

I've got somehow unexpected behaviour of std::swap with multi_array's
subarray. For example, given a 2x2 multi_array:

        boost::multi_array<double, 2> a;

        a.resize( boost::extents[2][2] );

        for (int i = 0; i < 2; ++i)
                for (int j = 0; j < 2; ++j)
                        a[i][j] = (i+1)*10 + j+1;

        std::swap( a[0], a[1] );

        for (int i = 0; i < 2; ++i)
        {
                for (int j = 0; j < 2; ++j)
                        std::cout << a[i][j] << " ";
                std::cout << std::endl;
        }

With msvc 2008 + boost 1.42, one of the rows overwrites the other and
the output is:

21 22
21 22

The fastest hacky solution i've came is this:

namespace std
{
template <typename TValue, int K>
void swap( boost::detail::multi_array::sub_array<TValue, K> &lhs,
         boost::detail::multi_array::sub_array<TValue, K> &rhs)
{
        boost::multi_array<TValue, K> tmp = lhs;
        lhs = rhs;
        rhs = tmp;
}
} // namespace std

Now, the rows are properly swapped and the output is:

21 22
11 12

Well, probably that's not a problem for now, but I dunno if it's a bug
or std::swap is not supported - coundn't find anything about this.

----------------
Sergey Mitsyn.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net