Boost logo

Boost Users :

Subject: Re: [Boost-users] [assign] Inconvenience with list_of
From: Thorsten Ottosen (thorsten.ottosen_at_[hidden])
Date: 2009-08-27 06:38:43


dariomt_at_[hidden] skrev:
> Hi list,
>
> I found a small inconvenience while trying to use boost::assign::list_of
> with the following code
>
> #include <vector>
> #include <boost/assign/list_of.hpp>
>
> struct A
> {
> // no implicit conversion from int
> explicit A(int i) {}
>
> // constructor from two ints
> A(int i, int j) {}
> };
>
> void works_with_two_ints()
> {
> std::vector<A> v = boost::assign::list_of<A>(1,2)(3,4)(5,6);
> }
>
> #if 0
> void does_not_work_with_one_int()
> {
> std::vector<A> v = boost::assign::list_of<A>(1)(2)(3);
> }
> #endif
>
>
> Looking into the code, it seems as if assign_detail::generic_list<A> had
> two slightly different versions of operator() generated with some
> preprocessor magic
>
> // 1) accepts a fully constructed A
> generic_list& operator()( const A& u )
> {
> this->push_back( u );
> return *this;
> }
>
> // 2) accepts two generic paramenters forwarding them to A's two
> parameter constructor
> template <typename U, typename V>
> generic_list& operator()(const U& u, const V& v)
> {
> this->push_back( A(u,v) );
> return *this;
> }
>
> I'm missing another operator() that takes one generic parameter and
> forwards it to A's one parameter constructor, for the case where it is
> not implicitly convertible.
>
> // 3) accepts a generic paramenter forwarding it to A's one parameter
> constructor
> template <typename U>
> generic_list& operator()(const U& u)
> {
> this->push_back( A(u) );
> return *this;
> }
>
> That saves me from typing A's everywhere
>
> std::vector<A> v = boost::assign::list_of(A(1))(A(2))(A(3));
>
> Does this make sense?

I think it does. Instead of adding a new function, I tried to replace
the one argument one. I also tried adding both.

It seems to break gcc 4.4 though. Not vc9. So until someone has found a
way to make it work on gcc, I'm slightly concerned about adding it.

-Thorsten


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