move-semantics

Is declaring explicitly defaulted move constructor in every class that doesn't provide user-defined one a good practice?

不想你离开。 提交于 2019-12-12 22:13:20
问题 There are quite complex (for me) rules that define when implicitly defaulted move constructor is generated and when it is not generated. What I'm afraid of is that the default move constructor won't be generated. Also I'm afraid that I (or someone else) modify the class in the future and implicit move constructor will disappear. There is an "advice" that says "you can always explicitly invoke the default generation for functions that can be automatically generated with = default (that's what

r-value causes a warning without the use of std::move

女生的网名这么多〃 提交于 2019-12-12 21:21:48
问题 Can someone help me to understand why the following code causes a warning struct A { A() : _a( 0 ) {} const int& _a; }; int main() { A a; } with warning warning: binding reference member '_a' to a temporary value [-Wdangling-field] A() : _a( 0 ) {} but this code, where std::move is used to initialize the member _a , does not: struct A { A() : _a( std::move(0) ) {} const int& _a; }; int main() { A a; } Aren't 0 and std::move( 0 ) both r-values? 回答1: This is an expression: 0 It's a very small

Can you reuse a moved std::string? [duplicate]

穿精又带淫゛_ 提交于 2019-12-12 10:37:23
问题 This question already has answers here : Reusing a moved container? (3 answers) Closed 5 years ago . Given this example: std::vector<std::string> split(const std::string& str) { std::vector<std::string> result; std::string curr; for (auto c : str) { if (c == DELIMITER) { result.push_back(std::move(curr)); // ATTENTION HERE! } else { curr.push_back(c); } } result.push_back(std::move(curr)); return result; } Can I reuse the curr std:string? This snippet seems working: after curr is moved inside

Why throw local variable invokes moves constructor?

烂漫一生 提交于 2019-12-12 10:28:45
问题 Recently, I've "played" with rvalues to understand their behavior. Most result didn't surprize me, but then I saw that if I throw a local variable, the move constructor is invoked. Until then, I thought that the purpose of move semantics rules is to guarantee that object will move (and become invalid) only if the compiler can detect that it will not be used any more (as in temporary objects), or the user promise not to use it (as in std::move). However, in the following code, none of this

Are there any use cases for a class which is copyable but not movable?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 09:32:50
问题 After reading this recent question by @Mehrdad on which classes should be made non-movable and therefore non-copyable , I starting wondering if there are use cases for a class which can be copied but not moved . Technically, this is possible: struct S { S() { } S(S const& s) { } S(S&&) = delete; }; S foo() { S s1; S s2(s1); // OK (copyable) return s1; // ERROR! (non-movable) } Although S has a copy constructor, it obviously does not model the CopyConstructible concept, because that is in turn

Should use unique_ptr to more easily implement “move” semantics?

筅森魡賤 提交于 2019-12-12 08:59:34
问题 Edit: made Foo and Bar a little less trivial, and direct replacement with shared_ptr<> more difficult. Should unique_ptr<> be used as an easier way to implement move semantics? For a class like class Foo { int* m_pInts; bool usedNew; // other members ... public: Foo(size_t num, bool useNew=true) : usedNew(useNew) { if (usedNew) m_pInts = new int[num]; else m_pInts = static_cast<int*>(calloc(num, sizeof(int))); } ~Foo() { if (usedNew) delete[] m_pInts; else free(m_pInts); } // no copy, but

Why does resize() cause a copy, rather than a move, of a vector's content when capacity is exceeded? [duplicate]

牧云@^-^@ 提交于 2019-12-12 08:38:03
问题 This question already has answers here : How to enforce move semantics when a vector grows? (3 answers) Closed 6 years ago . Given class X below (special member functions other than the one explicitly defined are not relevant for this experiment): struct X { X() { } X(int) { } X(X const&) { std::cout << "X(X const&)" << std::endl; } X(X&&) { std::cout << "X(X&&)" << std::endl; } }; The following program creates a vector of objects of type X and resizes it so that its capacity is exceeded and

Move constructor for std::string from char*

China☆狼群 提交于 2019-12-12 07:28:01
问题 I have a function f returning a char* . The function documentation says: The user must delete returned string I want to construct a std::string from it. The trivial things to do is: char* cstring = f(); std::string s(cstring); delete cstring; Is it possibile to do it better using C++ features? I would like to write something like std::string(cstring) avoiding the leak. 回答1: std::string will make a copy of the null terminated string argument and manage that copy. There's no way to have it take

moving elements of an initialization_list considered dangerous?

半城伤御伤魂 提交于 2019-12-11 18:36:57
问题 Previously asked questions (1 and 2) on SO seem to suggest that applying std::move on elements of a std::initializer_list may lead to UB. In fact, std::initializer_list iterators prevent effective move because they refer to const elements. The reason is that compilers may use static read-only memory for storing the underlying array of an initializer_list. I think this view is too limiting and I wanna know if experts out here agree. IMO, the following code cannot possibly use the read-only

Is a move constructor/assignment needed for RVO to kick in in C++11?

倖福魔咒の 提交于 2019-12-11 16:12:55
问题 For example: In accepted answer https://stackoverflow.com/a/14623480/1423254, Does copy elision and RVO would still work for classes without move constructors? Yes, RVO still kicks in. Actually, the compiler is expected to pick: RVO (if possible) In accepted answer https://stackoverflow.com/a/38043447/1423254, Under non-guaranteed copy elision rules, this will create a temporary, then move from that temporary into the function's return value. That move operation may be elided, but T must