I have a std::uint64_t m_value field in my class. In the default constructor, I write in the initialization list : m_value(0) . I work with different compilers, while you can adjust the output of warnings in the project settings. Some do not react to such initialization in any way, some write something about type conversion.

The question is: do I have to continue writing this way, or should I explicitly prescribe a type conversion, for example : m_value((std::uint64_t)(0)) , or via static_cast ? What is the best way to do? I understand that the implicit type conversion is, of course, executed, but it may make sense for a person reading the code to show what was meant?

  • Which compiler cursed and what message? - αλεχολυτ

2 answers 2

There is no point in using a cast. 0 is a constant expression, it can be said, a “universal” literal for scalar and especially arithmetic types. In addition, all static objects are initialized by the compiler to zero, then using 0 in this context in the constructor initialization list looks quite natural and does not cause questions. On the contrary, the use of casting may raise questions why this is done.

    In my opinion, a matter of taste ...
    If this were a template - I would cast to type parameter T , but in your case, it is easier to use a literal of the appropriate type - well, there, 0llu ...