On Fri, 14 Jan 2011 09:57:43 -0700 Joshua Marsh <[email protected]> wrote:
> On Jan 13, 2011, at 10:21 PM, Nathan Blackham wrote: > >> Without an explicit cast, shouldn't that be fatal? As you say, it > >> isn't. On Ubuntu, even with gcc -Wall, I get no warning. > > > > I would expect it to be fatal as well. I am surprised that it > > isn't. > > It's not fatal because it's an implicit cast. Number conversion is > usually considered implicit in C++. The compiler may warn about the > loss of precision (in g++'s case it allows you to do that with > -Wconversion), but it should never be fatal. It's an implicit cast that downconverts your data, which can break it. That's why I think it should be fatal. Granted, neither C nor C++ is ADA or COBOL (for which, thank Murphy!), and you should be able to do "stupid" things like downconvert. But you should do them knowingly and with a warning to maintainers. Hence, an explicit cast. Even if the authors of your compiler think it's OK, you should still make the cast explicit as a warning to your successors. And you should document why you think it's OK. I have no problem with implicit upconversion because there is no chance of a loss of data. > > On Fri, Jan 14, 2011 at 07:37, Dave Smith <[email protected]> > wrote: > > > > The double is floored to the closest int, unless it's too large to > > fit. So, yes, it's dangerous. That's why I want the warning on all > > distros! C++ is too dangerous for the compiler to let this kind of > > code through without a mention. > > -Wconversion was specifically built for this purpose. I'm sorry the > libraries your are incorporating into your project cause problems with > this, but that's their problem, not g++. There must be enough > developers using g++ (or at least contributing to g++) that feel like > the conversion is more of a feature than a danger. Otherwise, the > -Wconversion would be on by default or included in -Wall. > > I personally like it the way it is. Implicit conversion is fairly > normal from the kernel projects I've worked on. If you really want to > be a stickler though and don't like -Wconversion, c++ offers explicit > casting. This will usually signify to someone whose looking at your > code that you are doing some magic. > > /* > PLUG: http://plug.org, #utah on irc.freenode.net > Unsubscribe: http://plug.org/mailman/options/plug > Don't fear the penguin. > */ -- Charles Curley /"\ ASCII Ribbon Campaign Looking for fine software \ / Respect for open standards and/or writing? X No HTML/RTF in email http://www.charlescurley.com / \ No M$ Word docs in email Key fingerprint = CE5C 6645 A45A 64E4 94C0 809C FFF6 4C48 4ECD DFDB /* PLUG: http://plug.org, #utah on irc.freenode.net Unsubscribe: http://plug.org/mailman/options/plug Don't fear the penguin. */
