Should I really massively enter an explicit keyword?

When I used (recently released) Cppcheck 1.69 on my code 1 it showed a lot of messages that I did not expect ;-) Disabling noExplicitConstructor showed that they were all of that kind.

But I found that I'm not the only one who has many new Cppcheck posts, look at the results of the LibreOffice analysis (which I can show publicly):

enter image description here

What would an experienced programmer do:

  • Suppress check?
  • Mass introduction of the explicit keyword

1 Edit: This, of course, is not my code , but with the code I have to work on, it is outdated code: a combination of C and C ++ in several (pre-) standard variants, and this is a rather large code base.

+7
c ++ explicit legacy-code cppcheck
source share
1 answer

In the past, I was bitten by performance hits represented by implicit conversions, as well as obvious errors. Therefore, I usually use explicit for all constructors that I do not want to participate in implicit conversions so that the compiler can help me catch my mistakes. And then I try to always add the "// implied" comment for ctors, where I explicitly intend to use them as implicitly converting ctors. I find this helps me write more correct code with less surprises.

... So I would say "yes, go add explicit " - in the end you will be glad you did - this is what I did when I found out about it, and I'm glad I did.

+8
source share

All Articles