Boost logo

Boost Users :

Subject: Re: [Boost-users] [review][assign] Formal review of Assign v2 ongoing
From: John Bytheway (jbytheway+boost_at_[hidden])
Date: 2011-06-23 18:45:18


On 21/06/11 23:05, Simonson, Lucanus J wrote:
> er wrote:
>> You say "I ordinarily only initialize containers to literals when
>> writing unit tests.". In this case, I think you are right that you
>> can't beat C++0x initializer lists. But, you still may need to fill a
>> container, as above. And also, consider the cases below:
>>
>> #include <vector>
>> #include <queue>
>> #include <string>
>> #include <tuple>
>> #include <boost/assign/v2/include/csv_deque_ext.hpp>
>>
>> int main()
>> {
>> typedef std::string s_;
>> {
>> typedef std::tuple<s_, int> t_;
>> typedef std::vector<t_> v_;
>> v_ v1 = {
>> t_( "a", 1 ),
>> t_( "b", 2 ),
>> t_( "c", 3 ),
>> t_( "d", 4 ),
>> t_( "e", 5 )
>> };
>> using namespace boost::assign::v2;
>> v_ v2 = converter(
>> csv_deque<t_, 2>( "a", 1, "b", 2, "c", 3, "d", 4, "e", 5)
>> );
>> }
>
> No, initializer lists can apply to the elements and be nested.
>
> v_ v1 = {
> { "a", 1 },
> { "b", 2 },
> { "c", 3 },
> { "d", 4 },
> { "e", 5 }
> };
>
> Arguably the extra {} around each vector element in the new
> initializer syntax is more readable than the proposed assign
> v2 syntax. Tuple and deque support initializer list as do
> all classes with constructors and structs.

For what it's worth, your particular example doesn't work in gcc 4.5
because the relevant constructor of std::tuple's is explicit (I wasn't
aware of this effect of marking a constructor explicit, and I'm quite
surprised...). The analogous example with std::pair works fine.

John Bytheway


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