|
Boost Users : |
Subject: [Boost-users] [assign] Inconvenience with list_of
From: dariomt_at_[hidden]
Date: 2009-08-26 12:50:55
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?
Thanks!
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