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
int main() { typedef std::string s_; { typedef std::tuple
t_; typedef std::vector 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 ( "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