Linked Questions

1 vote
2 answers
138 views

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 ...
DaedalusAlpha's user avatar
1 vote
0 answers
111 views

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"}; ...
josecgon's user avatar
0 votes
0 answers
61 views

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 ...
Spiros's user avatar
  • 2,406
1721 votes
16 answers
294k views

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 ...
James McNellis's user avatar
91 votes
7 answers
22k views

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....
jbgs's user avatar
  • 2,945
77 votes
4 answers
13k views

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 ...
Niall Douglas's user avatar
5 votes
4 answers
1k views

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); ...
Danny Cohen's user avatar
2 votes
2 answers
1k views

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 ...
Alex O's user avatar
  • 1,976
1 vote
1 answer
918 views

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 } {} ...
Happy Jerry's user avatar
1 vote
1 answer
766 views

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 ...
24n8's user avatar
  • 2,328
0 votes
1 answer
829 views

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 ...
Richard's user avatar
  • 47k
3 votes
4 answers
730 views

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 ...
Benjamin Bihler's user avatar
0 votes
1 answer
102 views

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 ...
PazO's user avatar
  • 1,486