[boost-users] [assign] std::vector initialisation by boost::assign::list_of

The code is compilable on msvc but is not on gcc-4.3:
#include

Looks like gcc is not smart enough to guess to call `operator std::vector<int>()` ..\example\ddd.cpp:12: error: call of overloaded 'vector(boost::assign_detail::generic_list<int>&)' is ambiguous ... stl_vector.h:241: note: candidates are: std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = int, _Alloc = std::allocator<int>] ... stl_vector.h:227: note: std::vector<_Tp, _Alloc>::vector(size_t, const _Tp&, const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>] ... stl_vector.h:215: note: std::vector<_Tp, _Alloc>::vector(const _Alloc&) [with _Tp = int, _Alloc = std::allocator<int>] -- Regards, Andrey

On Wed, Mar 17, 2010 at 1:32 PM, Andrey Torba
The code is compilable on msvc but is not on gcc-4.3:
But the problem occurs when i try to initialise std::vector in a class initialization list: class sss { std::vector<int> v; sss() : v(boost::assign::list_of(1)(2)) {} };
What happens if you call: sss() : v(boost::assign::list_of(1)(2).to_adapter()) {}
list_of implements 2 members: implicit conversion opertor to the container type and the to_adapter member. You can cast list_of instance to your vector<int> if that with adapter does not work. Other solution can be: If you need efficiency, you still can use the vector ctor which reserves the number of items in a vector and than copies the elements from the list. sss() : v(2) { v = list_of...; } Regards, Ovanes

Sorry, it must be to_container() and not to_adapter().
On Wed, Mar 17, 2010 at 1:51 PM, Ovanes Markarian
On Wed, Mar 17, 2010 at 1:32 PM, Andrey Torba
wrote: The code is compilable on msvc but is not on gcc-4.3:
But the problem occurs when i try to initialise std::vector in a class initialization list: class sss { std::vector<int> v; sss() : v(boost::assign::list_of(1)(2)) {} };
What happens if you call: sss() : v(boost::assign::list_of(1)(2).to_adapter()) {}
list_of implements 2 members: implicit conversion opertor to the container type and the to_adapter member. You can cast list_of instance to your vector<int> if that with adapter does not work. Other solution can be:
If you need efficiency, you still can use the vector ctor which reserves the number of items in a vector and than copies the elements from the list.
sss() : v(2) { v = list_of...; }
Regards, Ovanes

Actually I tested some of them with gcc and the only solution I came up to
resolve ambiguity is:
#include
Sorry, it must be to_container() and not to_adapter().
On Wed, Mar 17, 2010 at 1:51 PM, Ovanes Markarian
wrote: On Wed, Mar 17, 2010 at 1:32 PM, Andrey Torba
wrote: The code is compilable on msvc but is not on gcc-4.3:
But the problem occurs when i try to initialise std::vector in a class initialization list: class sss { std::vector<int> v; sss() : v(boost::assign::list_of(1)(2)) {} };
What happens if you call: sss() : v(boost::assign::list_of(1)(2).to_adapter()) {}
list_of implements 2 members: implicit conversion opertor to the container type and the to_adapter member. You can cast list_of instance to your vector<int> if that with adapter does not work. Other solution can be:
If you need efficiency, you still can use the vector ctor which reserves the number of items in a vector and than copies the elements from the list.
sss() : v(2) { v = list_of...; }
Regards, Ovanes

mmmm...
Actually, I would probably create an empty vector and use the list_of in the
constructor's body.
Another idea I just came up with, is to use an transforming iterator, which
iterates over the list_of instance and additionally can be compared with a
default constructed iterator instance. Smth like:
vector<int> v(my_iterator(list_of(1)(2)), my_iterator());
Regards,
Ovanes
On Wed, Mar 17, 2010 at 4:16 PM, Ovanes Markarian
Actually I tested some of them with gcc and the only solution I came up to resolve ambiguity is:
#include
#include <vector> #include <iterator> #include <algorithm> #include <iostream> using namespace std; using namespace boost; using namespace boost::assign;
int main(int argc, char* argv[]) { typedef vector<int> int_vec; int_vec v = list_of(1)(2); int_vec v2(2); v2=list_of(1)(2).to_container(v2);
ostream_iterator<int> oiter(cout, ","); copy(v2.begin(), v2.end(), oiter); }
Hope that helps,
Ovanes
On Wed, Mar 17, 2010 at 1:52 PM, Ovanes Markarian
wrote: Sorry, it must be to_container() and not to_adapter().
On Wed, Mar 17, 2010 at 1:51 PM, Ovanes Markarian
wrote:
On Wed, Mar 17, 2010 at 1:32 PM, Andrey Torba
wrote: The code is compilable on msvc but is not on gcc-4.3:
But the problem occurs when i try to initialise std::vector in a class initialization list: class sss { std::vector<int> v; sss() : v(boost::assign::list_of(1)(2)) {} };
What happens if you call: sss() : v(boost::assign::list_of(1)(2).to_adapter()) {}
list_of implements 2 members: implicit conversion opertor to the container type and the to_adapter member. You can cast list_of instance to your vector<int> if that with adapter does not work. Other solution can be:
If you need efficiency, you still can use the vector ctor which reserves the number of items in a vector and than copies the elements from the list.
sss() : v(2) { v = list_of...; }
Regards, Ovanes

On 17 March 2010 17:58, Ovanes Markarian
vector<int> v(my_iterator(list_of(1)(2)), my_iterator());
I think that result of `list_of` will be destroyed after `my_iterator(...)` call, so `my_iterator` will not be valid. -- Regards, Andrey

Hi Andrey,
you can pass list by value. Compiler should be smart enough to eliminate the
temporary.
On the other hand, if you vector is always constant consider using a fusion
vector.
You can also introduce a helper function, which fills the vector with the
desired list
and returns it:
struct sss
{
sss()
: v(vector_items)
{}
private:
// this function should be inlined with temporary elimination
static vector<int> vector_items()
{
vector<int> v = list_of...;
return v;
}
};
Hope that helps,
Ovanes
On Wed, Mar 17, 2010 at 8:30 PM, Andrey Torba
On 17 March 2010 17:58, Ovanes Markarian
wrote: vector<int> v(my_iterator(list_of(1)(2)), my_iterator());
I think that result of `list_of` will be destroyed after `my_iterator(...)` call, so `my_iterator` will not be valid.
-- Regards, Andrey _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Andrey Torba
-
Ovanes Markarian