4

Is there a program that can scan entire projects and report all constructors eligible for implicit conversion?

Thanks!

pic11
  • 14,267
  • 21
  • 83
  • 119
  • How many constructors are you talking about? Could just do a find-in-files with a regex (ctors typically are `id::id`, so most of them are easy to find). – ssube Apr 20 '11 at 20:12
  • @peachykeen: Not constructors defined inside the class body. – Ben Voigt Apr 20 '11 at 20:29
  • @peachykeen All projects I coded in the past, coding now and will code in the future. I wish all constructors were explicit by default and implicit conversions didn't take place unless explicitly enabled. – pic11 Apr 20 '11 at 20:44
  • @Ben Voigt: In the headers, true. But in the code file they'll usually fit that pretty closely, no? Not an optimal method, but could save some searching if no app is available. – ssube Apr 20 '11 at 20:47
  • @peachykeen: Constructors defined inline in the class body don't have a separate definition in the implementation file. – Ben Voigt Apr 20 '11 at 20:54
  • @pic11: `I wish all constructors were explicit by default` - me too. I currently prefix *every* ctor with `explicit`, no matter how many arguments it takes (except for ctors that I really want implicit of course). Looks funny at first, but I have the impression that it really saves time. Not having to pay attention to the number of arguments that have no default when writing new ctors or modifying old ones really frees up some neurons for more important stuff. And of course it reduces the chance of forgetting to add `explicit` e.g. when adding default values. – Paul Groke Apr 20 '11 at 20:54
  • Thought to add a refrence to a question that introduces why we should consider this pattern in the first place: https://stackoverflow.com/q/121162/1971003 – Guy Avraham Aug 24 '17 at 06:50

1 Answers1

3

Cppcheck may be able to do this, but I'm not certain. If it doesn't do it now, it would be an ideal framework on which to build such a feature.

(Also, if you're not yet using Cppcheck, start now! Disclaimer: I'm a contributor.)

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • I use cppcheck and like it. Will try it with some decoy constructors. – pic11 Apr 20 '11 at 20:40
  • On second thought, cppcheck couldn't reasonably do this within the bounds of what cppcheck is designed to do. While checking a class declaration, obviously it doesn't have access to all the places where the class is used, so it couldn't tell whether a non-explicit constructor was needed or not. However, cppcheck *could* be modified to check for this, but would simply raise false positives for classes that actually need a non-explicit constructor. – Greg Hewgill Apr 20 '11 at 22:32
  • @GregHewgill I use it (Cppcheck) and it gave me this warning. My question, though is, why is it a warning , and how can I resolve it ? Thanks !! – Guy Avraham Aug 24 '17 at 06:29
  • 1
    @GuyAvraham: You can resolve it by making all single argument constructors `explicit`. It is a warning because non-explicit single argument constructors can be called for implicit type conversions, which can be very confusing and not often necessary. – Greg Hewgill Aug 24 '17 at 06:46
  • @GregHewgill -Thanks again !! Just saw a related question (with its answers) that introduces the problem rises with the error in this thread - so I will add a link here as well. – Guy Avraham Aug 24 '17 at 06:49