|
[Rivet] Dangerous casting to FinalStateDavid Bjergaard david.bjergaard at gmail.comTue Mar 10 15:23:55 GMT 2015
Hi Frank, Just thinking out loud: addProjection is templated on a reference, is it possible to make a specialized template for the non-reference version? Something like: template <typename PROJ> const PROJ& addProjection(const PROJ proj, const std::string& name) { std::cerr<<"Please be careful about using references!"<<std::endl; return proj; } I dunno if that will even compile. If its not too ugly, I think you could do something with pointers instead of references, I know you can properly specialize on those cases. Dave Frank Siegert <frank.siegert at cern.ch> writes: > Hi Riveters, > > just a short warning about a tricky mistake that can happen quite easily. > > Example1: > > FinalState remainder = zfinder.remainingFinalState(); > addProjection(remainder, "RFS"); > > Example2a: > > const FinalState& remainder = zfinder.remainingFinalState(); > addProjection(remainder, "RFS"); > > Example2b: > > addProjection(zfinder.remainingFinalState(), "RFS"); > > Example 2c: > > VetoedFinalState remainder = zfinder.remainingFinalState(); > addProjection(remainder, "RFS"); > > > All of them will compile fine, but the first one will behave > differently from the others. Since the object is copied instead of > passed by reference the remainder object will actually not be aware of > its "VetoedFinalState" properties and thus not apply any vetoing at > all. > > This is all fine, and Rivet is behaving correctly as expected from > C++, but it's nevertheless tricky to overlook. Does anybody have an > idea how to avoid this for users who don't pay close enough attention? > Is there any reasonable way of hiding/masking the copy constructor for > such cases? > > Cheers, > Frank > _______________________________________________ > Rivet mailing list > Rivet at projects.hepforge.org > https://www.hepforge.org/lists/listinfo/rivet
More information about the Rivet mailing list |