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

Consider passing TString by value to TNamed ctor #15434

Open
ChristianTackeGSI opened this issue May 7, 2024 · 6 comments · May be fixed by #15469
Open

Consider passing TString by value to TNamed ctor #15434

ChristianTackeGSI opened this issue May 7, 2024 · 6 comments · May be fixed by #15469
Assignees

Comments

@ChristianTackeGSI
Copy link
Collaborator

Explain what you would like to see improved and how.

The TNamed ctor currently takes TString by const reference:

TNamed::TNamed(const TString& name, const TString& title)

Reading https://www.cppstories.com/2018/08/init-string-member/ it seems, that it would be a good idea to instead pass it by value and then use move in the ctor?

-   TNamed(const TString &name, const TString &title) : fName(name), fTitle(title) { }
+   TNamed(TString name, TString title) : fName(std::move(name)), fTitle(std::move(title)) { }

ROOT version

master

Installation method

none, just looking at the source code.

Operating system

any

Additional context

We're currently looking at improving the ctors of our classes and noticed the double construction of TString, if we don't pass as const char*.

@guitargeek
Copy link
Contributor

Nice idea! Can you open a PR for that? This is a very common case indeed, it's nice to optimize it.

By the way, there are clang-tidy checks to do this automatically.

@ChristianTackeGSI
Copy link
Collaborator Author

Nice idea! Can you open a PR for that? This is a very common case indeed, it's nice to optimize it.

Yes, I could. But not before next week (I am quite busy this week).

By the way, there are clang-tidy checks to do this automatically.

Oh, I didn't know. Which ones do you mean?

@pcanal
Copy link
Member

pcanal commented May 7, 2024

Note that to be effective this change would also require the addition of a move constructor in TString.

@dennisklein
Copy link
Contributor

By the way, there are clang-tidy checks to do this automatically.

Oh, I didn't know. Which ones do you mean?

This looks like one: https://clang.llvm.org/extra/clang-tidy/checks/modernize/pass-by-value.html

@ChristianTackeGSI
Copy link
Collaborator Author

Note that to be effective this change would also require the addition of a move constructor in TString.

I think this exists already?
https://root.cern.ch/doc/master/classTString.html#a97795e61556ec0ced81e15cdfa26a538

TString::TString(TString&& s)

@pcanal
Copy link
Member

pcanal commented May 8, 2024

Indeed. I missed it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants