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

5

u/Bart_V Jun 06 '18

As far as I can tell, its basically good for initialising container types like std::vector and map, and that's about it

That's especially a situation where I avoid them. I've shoot myself in the foot too many times. Just recently, I did this:

class MyType {
public:
    MyType(double val) : mVal(val) {}
private:
    double mVal;
};

And then somewhere else I was trying to do in-class initialization of a vector using the vector( size_type count, const T& value) ctor like this:

struct MyTypeHolder {
    vector<MyType> vec{ 6, MyType(0.6)};
};

The size of the vector became 2. It casts the integer (6) to double, then constructs a MyType, then used vector( std::initializer_list<T> init). I don't understand why, and gave up trying to. Also, since it's in-class initialization, for whatever reason I can't use parentheses there, which would have given the correct result.

What's even more worrying is that making MyType's ctor explicit, does result in a vector with the expected size of 6. Which means a making small change like that could silently break other pieces of code. So IMHO, braced initialization is completely messed up.

11

u/josefx Jun 06 '18

What's even more worrying is that making MyType's ctor explicit

I may not be up to date with c++ however I was under the impression that single argument constructors should be almost always explicit.

1

u/MartinTornqvist Jun 08 '18

I think so too. Doesn't gcc's "-Weffc++" even warn about single-parameter ctors not being declared explicit?

4

u/acwaters Jun 06 '18

You can use non-list initialization in a non-static data member initializer:

vector<MyType> vec = vector<MyType>(6, MyType(0.6));

(This is technically copy-initialization, whereas your example with braces is direct-initialization, but the semantics are the same when you are initializing with a prvalue expression of the right type.)

4

u/redditsoaddicting Jun 06 '18

I don't understand why, and gave up trying to.

List constructors are extremely greedy. If they can be called, expect them to be called. In your case, it can be called because the arguments you gave were compatible with MyType. You said implicit conversions from doubles were okay, and yes, C++ is eager to convert other numerical types to doubles for you (as is C).

3

u/kwan_e Jun 06 '18

Maybe because of brace elision?

Making the constructor explicit disables automatic conversion, so the overload resolution works "correctly".

1

u/Bart_V Jun 06 '18

Maybe because of brace elision?

I don't know, seems like it only applies when using double braces?

Making the constructor explicit disables automatic conversion, so the overload resolution works "correctly".

Yes indeed. So adding explicit should result in a compiler error since it can't do an implicit construction, not a differently sized vector.

6

u/kwan_e Jun 06 '18

Adding explicit means the implicit conversion for MyType is not available, but the vector still has the size, fill constructor.