Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::assign::list_of and C++0x
From: Travis Gockel (travis_at_[hidden])
Date: 2011-06-03 01:29:00


Initializer lists adds language-level support for the same problem
assign::list_of solves.

The statement:
std::vector<int> vec = boost::assign::list_of(1)(2)(3);
Will have the functional equivalent in C++0x:
std::vector<int> vec = { 1, 2, 3};

There are some other differences, but the technical report is at
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2672.htm .
Wikipedia also has some good examples in their C++0x article.

On Thu, Jun 2, 2011 at 11:04 PM, asif saeed <asif.lse2_at_[hidden]> wrote:

> Hi, Boost::assign::list_of is absolutely wonderful. I used it in my last
> project. I was not aware of C++0x initializer lists. Is this something
> something similar to Assign::List of? Could you please give me a link to it?
> Thanks,Asif
>
> On Fri, Jun 3, 2011 at 9:48 AM, Travis Gockel <travis_at_[hidden]>wrote:
>
>> I'm moving a fairly large project over to C++0x, since it is about to be
>> published (crosses fingers) and we need to stay up with the times. In this
>> project, we use the wonderful boost::assign::list_of to assign collections
>> (since initializer lists don't exist yet). We are using g++ 4.5.1 and Boost
>> 1.46.1.
>>
>> When switching to C++0x, bits of code like this break:
>>
>> std::vector<int> vec;
>> vec = boost::assign::list_of(1)(2)(3);
>>
>> with the following error:
>>
>> break_list_of.cpp:7:41: error: ambiguous overload for ‘operator=’ in ‘vec
>> = ((boost::assign_detail::generic_list<int>*)boost::assign::list_of(const
>> T&) [with T = int]().boost::assign_detail::generic_list<T>::operator() [with
>> T = int, boost::assign_detail::generic_list<T> =
>> boost::assign_detail::generic_list<int>,
>> boost::assign_detail::generic_list<T>::Ty = int](((const
>> boost::assign_detail::generic_list<int>::Ty&)((const
>> boost::assign_detail::generic_list<int>::Ty*)(&2)))))->boost::assign_detail::generic_list<T>::operator()
>> [with T = int, boost::assign_detail::generic_list<T> =
>> boost::assign_detail::generic_list<int>,
>> boost::assign_detail::generic_list<T>::Ty = int](((const
>> boost::assign_detail::generic_list<int>::Ty&)((const
>> boost::assign_detail::generic_list<int>::Ty*)(&3))))’
>> /usr/include/c++/4.5/bits/vector.tcc:156:5: note: candidates are:
>> std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const
>> std::vector<_Tp, _Alloc>&) [with _Tp = int, _Alloc = std::allocator<int>]
>> /usr/include/c++/4.5/bits/stl_vector.h:336:7: note:
>> std::vector<_Tp, _Alloc>& std::vector<_Tp,
>> _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = int, _Alloc =
>> std::allocator<int>, std::vector<_Tp, _Alloc> = std::vector<int>]
>> /usr/include/c++/4.5/bits/stl_vector.h:357:7: note:
>> std::vector<_Tp, _Alloc>& std::vector<_Tp,
>> _Alloc>::operator=(std::initializer_list<_CharT>) [with _Tp = int, _Alloc =
>> std::allocator<int>, std::vector<_Tp, _Alloc> = std::vector<int>]
>>
>> The problem is that there are now multiple ways to assign to vectors (and
>> all the other collection types). Ideally, I would replace all usage of
>> boost::assign::list_of with C++0x initializer lists, but, alas, I am lazy.
>> Furthermore, I think I know how to fix this (see example below) and think
>> the Boost community would benefit from having boost::assign::list_of work in
>> C++0x and C++.
>>
>> template <typename T>
>> struct is_initializer_list : boost::false_type { };
>>
>> template <typename T>
>> struct is_initializer_list<std::initializer_list<T> > : boost::true_type {
>> };
>>
>> template <typename T>
>> struct wrapped_type
>> {
>> wrapped_type(const T& val) :
>> _val(val)
>> { }
>>
>> template <typename TOut,
>> // use new support for default template arguments in
>> functions
>> typename TPhony = typename
>> boost::disable_if<is_initializer_list<TOut> >::type
>> >
>> operator TOut() const
>> {
>> return _val;
>> }
>>
>> private:
>> T _val;
>> };
>>
>> I was wondering if anybody has solved this problem (or a similar one)
>> already. I would patch it myself, but I only have g++ and clang to test it
>> on. Also, there is a high probability that my solution is flawed in some
>> way.
>>
>> Thanks for the help! And, as always, thanks for the wonderful library.
>>
>> --
>> - Travis Gockel
>>
>> _______________________________________________
>> Boost-users mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
- Travis Gockel


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