|
[Rivet] Dangerous casting to FinalStateFrank Siegert frank.siegert at cern.chWed Jul 8 17:22:27 BST 2015
Hi Chris and David, thanks for taking a look and writing the summary. Here are my comments: > We both agree that the best thing to do is to have remainingFinalState() > return "const VetoedFinalState&", which is the actual type of the underlying > returned object. Currently it returns "const FinalState&". I agree, and this is part of the patch I posted in this thread. > This change > *should* generate compile-time warnings when a user tries something like > > FinalState fs = zfinder.remainingFinalState(); > > because fs is being copy-constructed from a more complex class. @Frank, I > think this will also make your attached broken example work as expected. Is this a compile-time warning or error, and are you sure it will really complain? I can test it myself later, but maybe you have already tested it. I doubt it is an error, and can't remember whether it even gave a warning, so in my patch I went the extra mile in the interest of our users (heh, I should be writing commercials ;-)) and did hide the relevant functions. > The following will work correctly and, now that remainingFinalState() > returns the correct type, is hopefully what users will do if they absolutely > have to make a copy. > > VetoedFinalState vfs = zfinder.remainingFinalState(); > > David's opinion (and he's convinced me, too) is that object slicing is an > inherent "feature" of c++ that is going to be very difficult to work around, > so just being careful with return types is the right way to deal with it. If us being careful with the return type is enough to avoid users accidently making this mistake (and we can argue whether ignoring a compile time warning still counts as accidental ;-)) this would be fine with me. Cheers, Frank
More information about the Rivet mailing list |