Competing constructors—Andrzej Krzemieński

Save to:
Instapaper Pocket Readability

We all got some surprises like this:

Competing constructors

By Andrzej Krzemieński

From the article:

We start with a known C++ gotcha:

std::vector<int> v (size_t(4), 2); // parentheses
std::vector<int> u {size_t(4), 2}; // braces
assert (v.size() == 4);
assert (u.size() == 2);
assert (v[0] == 2); // elements: {2, 2, 2, 2}
assert (u[0] == 4); // elements: {4, 2}

In this post I want to analyze the source of the problem a bit further, and offer some suggestions on class design.

Add a Comment

You must sign in or register to add a comment.

Comments (0)

There are currently no comments on this entry.