C++11 initializer lists ugly?

const CodeType dms = std::numeric_limits<CodeType>::max();

versus

const CodeType dms {std::numeric_limits<CodeType>::max()};

Which one would you use in your code?
Last edited on
closed account (zb0S216C)
Neither. I'm too stubborn. But to be fair, initialiser-lists in general look quite neat if you format them properly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct POD
{
  int Int_;
  double Double_;
};

// Neat:
POD PODA_ = {10, 10.0};

// Spread over too much area:
POD PODB_ =
{
  10,
  10.0
};

Wazzak
Neither. I always surround the two brackets in parentheses.

1
2
3
4
5
6
7
8
9
struct POD {
	int number_one, number_two;
};

int main(void) {
	POD pod({ 2, 5 });
        std::vector<int> numbers({ 5, 10, 15 });
	return 0;
}
Last edited on
Uniform initialization?

Like: It makes the 'most vexing parse' problem history.
1
2
3
4
struct A { A() ; /* ... */ } ; struct B { B(A) ; /* ... */ } ;

B foo( A() ) ; // declares a function
B bar{ A{} } ; // defines an object 



Like: I can now think of initializing an object logically rather than syntactically; for instance as "an object that can be initialized with five integers".
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <vector>
#include <tuple>
#include <iostream>

template < typename T > T get_numbers() { return T { 0, 1, 2, 3, 4 } ; }

int main()
{
    auto vector = get_numbers< std::vector<int> >() ;

    auto tuple = get_numbers< std::tuple<char,short,int,long,long long> >() ;

    struct B { int a, b ; long c, d ; short e ; } ;
    B b = get_numbers<B>() ;
}



Don't like: Uniform initialization can't be used as a drop-in replacement for all initialization; it clashes with initializer lists.
1
2
3
4
5
6
7
8
9
10
11
#include <vector>
#include <iostream>

int main()
{
    std::vector<int> one( 10, 7 ) ;
    std::cout << one.size() << ' ' << one.front() << '\n' ; // 10 7

    std::vector<int> two{ 10, 7 } ;
    std::cout << two.size() << ' ' << two.front() << '\n' ; // 2 10
}
Last edited on
Topic archived. No new replies allowed.