vendredi 15 juin 2018

Is it best practice to use a initializer_list in assignment

I have read (Bjarne Stroustrup, The C++ Programming Language, 6.3.5) about using initializer_list when initializing a variable, so that you don't have a narrowing conversion. Bjarne recommends only using direct-list-initialization :

X a1 {v};

X a2 = {v};

X a3 = v;

X a4(v);

Of these, only the first can be used in every context, and I strongly recommend its use. It is clearer and less error-prone than the alternatives.

Why does Bjarne only recommend the first one?

Why isn't it recommended to do initializer_list assignment (rather than initialization)? Or is it just implied that you should do that?

a1 = {v};

Also, why is it not valid syntax to do an assignment like this?

a1 {v};

Aucun commentaire:

Enregistrer un commentaire