-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RF] Don't allow passing temporaries to RooArgList/Set constructors #8065
Conversation
Starting build on |
Starting build on |
Starting build on |
Build failed on ROOT-debian10-i386/cxx14. Failing tests: |
Starting build on |
Build failed on ROOT-debian10-i386/cxx14. Errors:
|
Starting build on |
Build failed on ROOT-debian10-i386/cxx14. Errors:
|
Build failed on ROOT-ubuntu16/nortcxxmod. Failing tests: |
roofit/roofitcore/inc/RooArgList.h
Outdated
/// RooAbsArg as r-value reference is not allowed. | ||
template<typename... Arg_t> | ||
RooArgList(RooAbsArg && arg, Arg_t &&... argsOrName) | ||
: RooArgList{arg, std::move(arg), std::forward<Arg_t>(argsOrName)...} {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really std::move(arg)
, not forward
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would stay std::move
, because here the RooAbsArg && arg
is not a universal reference but an r-value reference.
I I think the more general std::forward
is intended to forward universal references correctly, but here I explicitly create a new r-value reference from arg
, which is what move
does. Of course they both work, if you want I can change :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(rebasing and solving conflicts in this PR at the moment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, so you want temporaries all the way in the arguments? That might work indeed.
The two classes inheriting from RooAbsCollection, namely RooArgList and RooArgSet are by default non-owning collections. This means they should not be initialized with temporary objects, otherwise they contain invalid pointers right after construction. To avoid that this can ever happen, the universal reference mechanism is used to statically check for temporaries, such that your code doesn't even compile if you try to construct a RooArgList or RooArgSet from temporaries.
Starting build on |
Build failed on ROOT-debian10-i386/cxx14. Errors:
|
Build failed on ROOT-ubuntu16/nortcxxmod. Failing tests: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice addition to avoid the problem of temporaries.
I think the PR is good to be merged.
The two classes inheriting from RooAbsCollection, namely RooArgList and
RooArgSet are by default non-owning collections. This means they should
not be initialized with temporary objects, otherwise they contain
invalid pointers right after construction.
To avoid that this can ever happen, the universal reference mechanism is
used to statically check for temporaries, such that your code doesn't
even compile if you try to construct a RooArgList or RooArgSet from
temporaries.
This fixes #8052.
Some C++ code to validate that the
static_assert
works: