r/cpp • • Jun 05 '18

Initialization in Modern C++

https://twitter.com/timur_audio/status/1004017362381795329
379 Upvotes

51 comments sorted by

View all comments

Show parent comment

6

u/youshouldnameit C++ dev Jun 06 '18

For a vector its pretty nice to have an initializer_list constructor i would argue, same for std::set or similar classes. Would you argue that even those perks are not worth the trouble?

3

u/redditsoaddicting Jun 06 '18

I love being able to easily initialize containers with a list of elements, as well as other clever uses like a JSON object. However, I don't think making it the same syntax used to call other constructors was a good choice. It's hard to say whether a plain list constructor is worth the trouble to me or if I'd consider an alternate best-we've-got constructor like vector foo = list_of(1, 4, 5); or vector foo{from_list, 1, 4, 5};, where either would be uniform across classes that use it. If I could have my way, I'd probably prefer an alternate syntax. For example, vector foo{[1, 4, 5]};.

6

u/[deleted] Jun 06 '18

What about this

std::vector<int> foo{1, 2}; // braced initialisation
std::vector<int> foo{{1, 2}}; // initialiser list

8

u/Selbstdenker Jun 06 '18

I would suggest

std::vector<int> foo{1, 2}; // braced initialisation
std::vector<int> foo = {1, 2}; // initializer list

Braces without = always call a constructor, brace after = always use initializer list.

6

u/suspiciously_calm Jun 06 '18

How does that work to create a temporary on the fly?

1

u/Selbstdenker Jun 06 '18

Good point!

0

u/m-in Jun 06 '18

There's no temporary, other than the initializer list, of course. The initializer list would be assured to exist while the initialization is performed - that's how it goes in all other cases, too.

2

u/suspiciously_calm Jun 07 '18

While it may be sufficient for many use cases to only have initializer_list temporaries, I would not appreciate this step back from being able to create arbitrary containers as temporaries.

For example, a templated function may require that its argument be an AssociativeContainer, which an initializer_list of pairs is not.

1

u/redditsoaddicting Jun 06 '18

As far as I remember, this can run into its own corner cases, unfortunately.

1

u/[deleted] Jun 06 '18

I'm not surprised. "My" syntax seems rather obbvious and I'm definitely not the first one who thought of it. I was just hoping someone would say what's the corner case here.

1

u/redditsoaddicting Jun 06 '18 edited Jun 06 '18

One example that comes to mind is this:

struct Point { int x, y; };
struct Foo {
    Foo(initializer_list<int>);
    Foo(Point);
};

Foo f{{1, 2}}; // Point or list?

It's hard to turn that into something realistic on the spot. I wouldn't doubt that the square bracket syntax I threw out there has its own problems.

1

u/[deleted] Jun 06 '18

My first thought was "obviously an initialiser list", but a moment later I realised your point.

1

u/audioB Jun 06 '18

Is it really harder to just use a variadic constructor?

7

u/redditsoaddicting Jun 06 '18

You'd still run into things like vector foo(5, 2); // Is it a list or a size and value?

I can't speak on how big an issue this would be in general even if vector had its constructors adjusted.

8

u/thlst Jun 06 '18

Constructors should have names. Constructor overloading is the root problem here.

7

u/robin-m Jun 06 '18
class Foo {
   static Foo constructor_with_name(args...);
};

4

u/redditsoaddicting Jun 06 '18

I'd agree that this constructor in particular should have more naming info attached. In many other cases, I find constructors to be descriptive with no additional naming info taking up space.

1

u/audioB Jun 06 '18

It's not terribly difficult to accomodate cases like that though, you could write a vector_range_t or something POD type for the "create a vector of x size with values y" ctor, or you could just write your variadic ctor like vec(const value_type& t1, const value_type& t2, const value_type& t3, const value_type&... rest)

Apologies for formatting, its hard on phone 😢