Boost logo

Boost :

From: Sylvain Pion (Sylvain.Pion_at_[hidden])
Date: 2006-08-09 04:31:02


Jeff Flinn wrote:
> Would not the boost.assign library work here?
>
>>>class A {
>>> boost::array<int, 3> a;
>>> A() : a(boost::assign::list_of(1)(2)(3)(4)) {}
>>>};
>
> I don't think I've tried it this way yet.

It works, but it's not efficient. It is even incredibly worse that
doing the assignments by hand (boost 1.33.1 is better than 1.32 though).
See my attached test program. Here is what I get (with g++ 4.1):

                      #default ctor | #copy ctor | #operator= | #dtor
make_array 1 | 3 | 0 | 4
hand assignments 4 | 0 | 3 | 4
assign::list_of 1.32 4 | 11 | 3 | 15
assign::list_of 1.33.1 4 | 7 | 3 | 11

-- 
Sylvain

#include <boost/array.hpp>
#include <boost/assign.hpp>
#include <boost/version.hpp>
#include <iostream>

struct B
{
  B() { std::cout << "default ctor" << std::endl; }
  B(const B&) { std::cout << "copy ctor" << std::endl; }
  B& operator=(const B&) { std::cout << "assign operator" << std::endl; return *this; }
  ~B() { std::cout << "destructor" << std::endl; }
};

template < typename T > inline
boost::array<T, 3>
make_array(const T& b1, const T& b2, const T& b3)
{
  boost::array<T, 3> a = { { b1, b2, b3 } };
  return a;
}

typedef boost::array<B, 3> Array;

struct A {
  Array array;
  A(const B &b1, const B &b2, const B &b3)
    : array(make_array(b1, b2, b3)) {}
  //{ array[0] = b1; array[1] = b2; array[2] = b3; }
    //: array(boost::assign::list_of(b1)(b2)(b3)) {}
    //: array(b1, b2, b3) {} // not [yet] C++
};

int main()
{
  std::cout << "Using Boost version " << BOOST_LIB_VERSION << std::endl;
  B b;
  A a(b, b, b);
}


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