Skip to content

[RF] Don't allow passing temporaries to RooArgList/Set constructors#8065

Merged
guitargeek merged 1 commit intoroot-project:masterfrom
guitargeek:issue-8052
Jul 21, 2021
Merged

[RF] Don't allow passing temporaries to RooArgList/Set constructors#8065
guitargeek merged 1 commit intoroot-project:masterfrom
guitargeek:issue-8052

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

}

@guitargeek guitargeek requested a review from hageboeck as a code owner April 30, 2021 16:26
@guitargeek guitargeek self-assigned this Apr 30, 2021
@guitargeek guitargeek requested a review from lmoneta as a code owner April 30, 2021 16:26
@root-project root-project deleted a comment from phsft-bot May 26, 2021
@root-project root-project deleted a comment from phsft-bot May 26, 2021
@root-project root-project deleted a comment from phsft-bot May 26, 2021
@root-project root-project deleted a comment from phsft-bot May 26, 2021
@root-project root-project deleted a comment from phsft-bot May 26, 2021
@root-project root-project deleted a comment from phsft-bot May 26, 2021
@phsft-bot
Copy link

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

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

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

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

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

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

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

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

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

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

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

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