Boost logo

Boost :

From: Shawn D. Pautz (sdpautz_at_[hidden])
Date: 2007-11-28 11:53:45


I believe that I have found a discrepancy between multi_array and its
online documentation. In the reference manual at
http://boost.org/libs/multi_array/doc/reference.html both the copy
constructor and the assignment operator are described as calling the
copy constructor of the stored type (element). No mention is made of
the assignment operator of element. But in the implementation it
appears that both functions call element's assignment operator (via
calls to std::copy or boost::detail::multi_array::copy_n). Furthermore
the multi_array assignment operator does not call element's copy
constructor at all.

Here is a simple program to illustrate the behavior:

>more ./testMultiArray.cc

#include <boost/multi_array.hpp>
#include <iostream>
using std::cout;
using std::endl;

class Foo
{
 public:
   Foo()
   { cout << " Foo default constructor" << endl; }
   Foo& operator=(const Foo& foo)
   { cout << " Foo operator=" << endl; return *this; }
   Foo(const Foo& foo)
   { cout << " Foo copy constructor" << endl; }
};

int main()
{
   cout << "Calling multi_array extents constructor" << endl;
   boost::multi_array<Foo, 1> myFooArray(boost::extents[2]);

   cout << "\n\nCalling multi_array copy constructor" << endl;
   boost::multi_array<Foo, 1> myFooArray2(myFooArray);

   cout << "\n\nCalling multi_array assignment operator" << endl;
   myFooArray2 = myFooArray;
}

Here are the runtime results (compiled with gcc 3.4.5 against Boost
1.34.1):

>./testMultiArray
Calling multi_array extents constructor
 Foo default constructor
 Foo copy constructor
 Foo copy constructor

Calling multi_array copy constructor
 Foo default constructor
 Foo copy constructor
 Foo copy constructor
 Foo operator=
 Foo operator=

Calling multi_array assignment operator
 Foo operator=
 Foo operator=

What are the intended requirements on the stored type? Does it need to
be Assignable? The above example works since Foo is Assignable, but for
other types (such as nested multi_arrays) the multi_array copy
constructor will not work. Could someone either fix multi_array or
update the documentation?

Shawn Pautz


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk