Skip to content
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

Merged
merged 1 commit into from
Jul 21, 2021

Conversation

guitargeek
Copy link
Contributor

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:

#include "RooRealVar.h"
#include "RooArgList.h"
#include "RooArgSet.h"
#include "RooUniform.h"

void test() {

    RooRealVar x("x", "x", 0, 0, 10);
    RooRealVar y("y", "y", 0, 0, 10);

    RooUniform u1("u1", "u1", x);
    RooUniform u2("u2", "u2", y);

    // this should work
    RooArgSet (u1, u2);
    RooArgList(u1, u2);

    // all of these combinations should cause compiler errors
    ///RooArgSet (RooUniform("u1", "u1", x), u2                       );
    //RooArgSet (u1,                        RooUniform("u2", "u2", y));
    //RooArgSet (RooUniform("u1", "u1", x), RooUniform("u2", "u2", y));
    //RooArgList(RooUniform("u1", "u1", x), u2                       );
    //RooArgList(u1,                        RooUniform("u2", "u2", y));
    //RooArgList(RooUniform("u1", "u1", x), RooUniform("u2", "u2", y));

}

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-fedora30/cxx14, ROOT-fedora31/noimt, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-debian10-i386/cxx14.
Running on pcepsft10.dyndns.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-debian10-i386/cxx14.
Running on pcepsft10.dyndns.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2021-07-06T10:25:55.753Z] CMake Error at /home/sftnight/build/workspace/root-pullrequests-build/rootspi/jenkins/root-build.cmake:1045 (message):

@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-debian10-i386/cxx14.
Running on pcepsft10.dyndns.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2021-07-06T10:27:05.590Z] CMake Error at /home/sftnight/build/workspace/root-pullrequests-build/rootspi/jenkins/root-build.cmake:1045 (message):

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu16/nortcxxmod.
Running on sft-ubuntu-1604-4.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

/// 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)...} {}
Copy link
Member

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?

Copy link
Contributor Author

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 :)

Copy link
Contributor Author

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)

Copy link
Member

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.
@phsft-bot
Copy link
Collaborator

Starting build on ROOT-debian10-i386/cxx14, ROOT-performance-centos8-multicore/default, ROOT-ubuntu16/nortcxxmod, mac1014/python3, mac11.0/cxx17, windows10/cxx14
How to customize builds

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-debian10-i386/cxx14.
Running on pcepsft10.dyndns.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Errors:

  • [2021-07-06T17:12:57.365Z] CMake Error at /home/sftnight/build/workspace/root-pullrequests-build/build/TBB-prefix/src/TBB-stamp/TBB-build-Release.cmake:49 (message):

@phsft-bot
Copy link
Collaborator

Build failed on ROOT-ubuntu16/nortcxxmod.
Running on sft-ubuntu-1604-2.cern.ch:/build/workspace/root-pullrequests-build
See console output.

Failing tests:

Copy link
Member

@lmoneta lmoneta left a 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.

@guitargeek guitargeek merged commit 6e5677f into root-project:master Jul 21, 2021
@guitargeek guitargeek deleted the issue-8052 branch July 21, 2021 16:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[RF] RooUniform in RooArgList in RooProdPdf: segmentation violation during fit
4 participants