Linked Questions
13 questions linked to/from Advantages of pass-by-value and std::move over pass-by-reference
1 vote
2 answers
138 views
Performance of passing std::string by value in constructor [duplicate]
Say I have this class: class MyClass { public: MyClass(std::string str) : m_str(std::move(str)) {} private: std::string m_str; } In modern C++, will this under any circumstance be less ...
1 vote
0 answers
111 views
Does C++ moves by default an string in a ctor? [duplicate]
Given this example: #include <string> class Foo { public: Foo(std::string p_member) : m_member{p_member} {} private: std::string m_member; }; int main() { Foo f{"Test"}; ...
0 votes
0 answers
61 views
Pass by value to avoid interface duplication [duplicate]
Suppose there is a copy--and-move-constructible (and assignable) object, such as std::vector<int>. A class of mine will accept on such object and store it as a member: class A { public: void ...
1721 votes
16 answers
294k views
What are rvalues, lvalues, xvalues, glvalues, and prvalues?
In C++03, an expression is either an rvalue or an lvalue. In C++11, an expression can be an: rvalue lvalue xvalue glvalue prvalue Two categories have become five categories. What are these new ...
91 votes
7 answers
22k views
Is the pass-by-value-and-then-move construct a bad idiom?
Since we have move semantics in C++, nowadays it is usual to do void set_a(A a) { _a = std::move(a); } The reasoning is that if a is an rvalue, the copy will be elided and there will be just one move....
77 votes
4 answers
13k views
Why is value taking setter member functions not recommended in Herb Sutter's CppCon 2014 talk (Back to Basics: Modern C++ Style)?
In Herb Sutter's CppCon 2014 talk Back to Basics: Modern C++ Style he refers on slide 28 (a web copy of the slides are here) to this pattern: class employee { std::string name_; public: void ...
5 votes
4 answers
1k views
Best way to create a setter function in C++
I want to write a template function that receives parameter by move or by copy. The most efficient way that I use is: void setA(A a) { m_a = std::move(a); } Here, when we use is A a; setA(a); ...
2 votes
2 answers
1k views
Pass-by-value and std::move vs forwarding reference
I encounter the pass by value and move idiom quite often: struct Test { Test(std::string str_) : str{std::move(str_)} {} std::string str; }; But it seems to me that passing by either const ...
1 vote
1 answer
918 views
Is there any point in returning an object (e.g., std::string) by reference when the method has no parameters?
Take the following snippet of code #include <iostream> #include <string> class Foo { private: std::string m_name; public: Foo(std::string name) : m_name { name } {} ...
1 vote
1 answer
766 views
Pass by value/reference for a parameter that will be moved within function
If you have a function parameter that is intended to be moved into a variable within the function, would you have want to use pass by reference instead of pass by value? For example, is there ever ...
0 votes
1 answer
829 views
Using std::move on std::shared_ptr<SomeClass> correctly
I am working on a project and dealing with shared_ptr's. The program is working and giving the correct output. However upon looking at the logs i notice that at some points in the program the ...
3 votes
4 answers
730 views
Is clang-tidy modernize-pass-by-value only right from C++20 on?
There are many questions and answers about whether to pass arguments by value or by reference. This answer https://stackoverflow.com/a/51706522/2492801 seems to indicate that passing by value and ...
0 votes
1 answer
102 views
How do I use lambda with temporary object implementing move semantics?
I am trying to write a lambda that receives a temporary object. The lambda will be cached for later execution, and will be executed after the original object was destructed (hence should make a copy ...