Comments (6)
Hi @beinhaerter, may I ask you double-check the snippet, please? I think that the
lvalue
in the laststd::cout
should bervalue
.
Thanks for pointing that out. I corrected the code snippet.
from cppcoreguidelines.
[...] I'd follow the approach in your first bullet point [...]
Taking the argument by reference to const and then creating a copy is less efficient
if the caller passes an rvalue to the function
because that creates copy, whereas taking the argument by value would move.
from cppcoreguidelines.
Hi, I can't sure what you are missing or misunderstanding, although your explanations dealing with rvalue (reference(s)) are not clear to me. This also pertains your third and fourth questions.
I don't think your use case is so exotic and to comply with the guideline as it is I'd follow the approach in your first bullet point: I think that, if I am understanding you correctly,
auto f(T const& t) {
// This would be what you called little bit more code, correct? Strictly, with the const& too.
auto t_copy = t;
...
}
is not in fact "tiny bit more"(?) complicated code than
auto f(T t) {
...
}
from cppcoreguidelines.
@iglesias siebenschlaefer said that the const&
code is less efficient when you pass an rvalue to it. You can compare it using the code below.
I agree with @siebenschlaefer and that is why I am using this pass-by-value approach in my code, too. Contradicting 'F.18: For “will-move-from” parameters, pass by X&& and std::move the parameter' I even use that for will-move-from parameters to give the caller of my function more freedom and to not require him to std::move()
.
#include <iostream>
struct S {
S() { std::cout << "ctor\n"; }
S(const S&) { std::cout << "copy ctor\n"; }
S(S&&) { std::cout << "move ctor\n"; }
};
S get() noexcept { return S{}; }
void fc(const S& s) { S copy{s}; }
void fr(S&& s) {}
void fv(S s) {}
int main() {
S s;
std::cout << "\nf taking const&, getting lvalue\n";
fc(s);
std::cout << "\nf taking const&, getting rvalue\n";
fc(get());
std::cout << "\n\nf taking rvalue\n";
fr(get());
std::cout << "\n\nf taking value, getting lvalue\n";
fv(s);
std::cout << "\nf taking value, getting rvalue\n";
fv(get());
}
from cppcoreguidelines.
Hi @beinhaerter, may I ask you double-check the snippet, please? I think that the lvalue
in the last std::cout
should be rvalue
.
Disregarding that and assuming the snippet's point is correct. I feel that better measurement is needed to reach the conclusion that it is less efficient.
from cppcoreguidelines.
Editors call: Thanks! We intend to update the wording to cover this case better.
from cppcoreguidelines.
Related Issues (20)
- File extensions for modules? HOT 2
- ES.30: Don’t use macros for program text manipulation - example warnings HOT 3
- tyu
- Create binary tree by array HOT 1
- ES.25 -- Is it truly beneficial to always employ const for objects? HOT 2
- Ranges require exception to F.19 HOT 7
- Wrong links in P.8 HOT 1
- Halo HOT 1
- Remove enforcement rules of rule F.17 HOT 7
- ES.79 is a terrible advice and should be dropped HOT 14
- C.21 Exception for copy-and-swap idiom HOT 4
- Broken links HOT 3
- F.20 should exclude swap HOT 3
- SF.2 allow inline variables and static inline class member variables HOT 1
- Poor (and nearly-incorrect) example for rule F.27 (shared_ptr) HOT 4
- Challenging C.153 "Prefer virtual function to casting" HOT 4
- ES.56 Clarification on moving when assigning to reference outside of scope HOT 3
- F.16 Allow pass-by-value for types that are copied HOT 2
- `gsl::suppress` issues collected: Strings, literals, cases HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppcoreguidelines.