back_inserter and pointer container library
Dear all, it seems that the pointer container library does not interfere well with back_inserter, i.e. boost::ptr_vector<int> v2; std::back_insert_iterator< boost::ptr_vector<int> > b(v2); b.operator=(new int); //forget it This is unfortunate, because now a lot of STL (or our own adapters) containers can't be used (e.g. std::transform, std::generate_n) Wkr, me
gast128 wrote:
Dear all,
it seems that the pointer container library does not interfere well with back_inserter, i.e.
boost::ptr_vector<int> v2; std::back_insert_iterator< boost::ptr_vector<int> > b(v2); b.operator=(new int); //forget it
[remark: I assume you only use int as example?]
This is unfortunate, because now a lot of STL (or our own adapters) containers can't be used (e.g. std::transform, std::generate_n)
This is because the concept of a PtrContainer is not the same as a Container. Specifically, back_inserter() will try to call push_back() which does not take a T& parameter, but a T* parameter. operator=() in back_insert_iterator will on the other take a T& parameter. After the review, the T& overloads for push_back() etc were removed because people felt they weren't needed. I see two ways to solve the problem: 1. overload push_back(), push_front(), insert() etc for T& parameters 2. provide a new function: template< class PtrCont > ptr_back_insert_iterator<PtrCont> ptr_back_inserter( PtrCont& c ) { return ptr_back_insert_iterator<PtrCont>(c); } template< class PtrCont > class ptr_back_insert_iterator<PtrCont> { ... ptr_back_insert_iterator& operator=( typename PtrCont::reference r ) { c_.push_back( new_clone(r) ); return *this; } }; Have I overlooked something? I think I would favor option 1. However, this should be discussed in the developer list first. I'm also planning to drop the forced non-copyability (by providing cc and ca) since we have discussed in a couple of times and both times I seemed to be the only one who favors the current design. -Thorsten
boost::ptr_vector<int> v2; [remark: I assume you only use int as example?] Yes. Of cours in 'real' programmers life, these are classes.
2. provide a new function: template< class PtrCont > class ptr_back_insert_iterator<PtrCont> {...}; I find this an appealing alternative.
gast128 wrote:
boost::ptr_vector<int> v2;
[remark: I assume you only use int as example?]
Yes. Of cours in 'real' programmers life, these are classes.
2. provide a new function: template< class PtrCont > class ptr_back_insert_iterator<PtrCont> {...};
I find this an appealing alternative.
It is certainly easier for you to add it than to wait for me to extend the interface of the containers. :-) OTOH, it is quite irritating that this assymmetry between input and output in the containers makes the containers less useable in such contexts. -Thorsten
participants (2)
-
gast128
-
Thorsten Ottosen