move-semantics

Why std::make_move_iterator works on vector<string> but not on vector<int>

筅森魡賤 提交于 2019-12-07 00:13:33
问题 I was expecting that std::make_move_iterator will always move contents, but it seems not. It looks like it is moving elements in vector<string> but not in vector<int> . See the below code snippet: #include <iostream> #include <iterator> #include <string> #include <vector> void moveIntVector() { std::cout << __func__ << std::endl; std::vector<int> v1; for (unsigned i = 0; i < 10; ++i) { v1.push_back(i); } std::vector<int> v2( std::make_move_iterator(v1.begin() + 5), std::make_move_iterator(v1

Unnecessary emptying of moved-from std::string

谁说我不能喝 提交于 2019-12-06 21:01:22
问题 Both libstdc++ and libc++ makes moved-from std::string object empty, even if the original stored string is short and short string optimization is applied. It seems to me that this emptying makes an additional and unnecessary runtime overhead . For instance, here is the move constructor of std::basic_string from libstdc++: basic_string(basic_string&& __str) noexcept : _M_dataplus(_M_local_data(), std::move(__str._M_get_allocator())) { if (__str._M_is_local()) traits_type::copy(_M_local_buf, _

Sink arguments and move semantics for functions that can fail (strong exception safety)

时间秒杀一切 提交于 2019-12-06 19:48:02
问题 I have a function that operates on a big chunk of data passed in as a sink argument. My BigData type is already C++11-aware and comes with fully functional move constructor and move assignment implementations, so I can get away without having to copy the damn thing: Result processBigData(BigData); [...] BigData b = retrieveData(); Result r = processBigData(std::move(b)); This all works perfectly fine. However, my processing function may fail occasionally at runtime resulting in an exception.

const_cast and std::move to remove constness from non-reference

跟風遠走 提交于 2019-12-06 18:31:44
问题 I have an external library which I can not modify. The library declares a template function that for some reason returns const non-reference object: template<class C> const C foo(); I have another external library which I can not modify too. The library declares a class that is non-copyable and has a move constructor from a non-const object only: struct bar { bar(); bar(const bar&)=delete; bar(bar&&); }; Now I need to use foo<bar> . A simple usage: bar buz() { return foo<bar>(); } fails with

std::vector initialization move/copy constructor of the element

一世执手 提交于 2019-12-06 16:05:26
I have this piece of code: #include <iostream> #include <vector> using namespace std; class Foo{ public: Foo() noexcept {cout << "ctor" << endl;} Foo(const Foo&) noexcept {cout << "copy ctor" << endl;} Foo(Foo&&) noexcept {cout << "move ctor" << endl;} Foo& operator=(Foo&&) noexcept {cout << "move assn" << endl; return *this;} Foo& operator=(const Foo&) noexcept {cout << "copy assn" << endl; return *this;} ~Foo() noexcept {cout << "dtor" << endl;} }; int main() { Foo foo; vector<Foo> v; v.push_back(std::move(foo)); // comment the above 2 lines and replace by // vector<Foo> v{std::move(foo)}; }

C++ move constructor not called for rvalue reference [duplicate]

半腔热情 提交于 2019-12-06 13:29:37
This question already has answers here : On how to recognize Rvalue or Lvalue reference and if-it-has-a-name rule (3 answers) Closed 3 years ago . class MyClass { public: MyClass() { std::cout << "default constructor\n"; } MyClass(MyClass& a) { std::cout << "copy constructor\n"; } MyClass(MyClass&& b) { std::cout << "move constructor\n"; } }; void test(MyClass&& temp) { MyClass a(MyClass{}); // calls MOVE constructor as expected MyClass b(temp); // calls COPY constructor... not expected...? } int main() { test(MyClass{}); return 0; } For the above code, I expected both object creations in test

C++11 - Distinguishing rvalue pointers

橙三吉。 提交于 2019-12-06 13:05:15
How can I distinguish a variable as a compiler-constructed string? For example, while the rvalue "Hello, World" is of type const char* . const char* in itself does not mean that a pointer can't be changed. A char* const pointer can't be changed, but that's not what's constructed by the compiler. Does this mean that, for any container that holds a const char* , the data should be copied by means other than C++'s move semantics? Is there any way to just move compiler-constructed strings and leave all other strings alone? For example, in the GCC 4.5.2, a method that returns the type int as

C++11: std::move() call on arguments' list

可紊 提交于 2019-12-06 13:02:32
Is it safe to operate on object within arguments' list, when there is also std::move() invoked on that object ? void foo(int* raw, std::unique_ptr<int> u) { *raw = 456; } std::unique_ptr<int> p(new int(123)); foo(p.get(), std::move(p)); Will the `raw' pointer in foo() be valid if std::move(p) was evaluated as the first parameter ? No, it's NOT safe. the eval order of argument is not specified in standard. So your code can be run as: std::move(p) . call move constructor of std::unique_ptr<int> . p.get() (because of 2., this will be nullptr .) and pass this parameter. call foo . You have to do

Move semantics clarification [duplicate]

一世执手 提交于 2019-12-06 09:39:43
This question already has answers here : Closed 6 years ago . I have read the below post which gives a very good insight into move semantics: Can someone please explain move semantics to me? but I am still fail to understand following things regarding move semantics - Does copy elision and RVO would still work for classes without move constructors? Even if our classes doesn't have move constructors, but STL containers has one. For operation like std::vector vt = CreateMyClassVector(); and to perform operations like sorting etc. Why can't STL internally leverage move semantics to improve such

C++11 Move semantics behaviour specific questions

↘锁芯ラ 提交于 2019-12-06 06:50:40
I have read the below post which gives a very good insight into move semantics: Can someone please explain move semantics to me? but I am still fail to understand following things regarding move semantics - Does copy elision and RVO would still work for classes without move constructors? Even if our classes doesn't have move constructors, but STL containers has one. For operation like std::vector<MyClass> vt = CreateMyClassVector(); and to perform operations like sorting etc. Why can't STL internally leverage move semantics to improve such operations internally using operations like copy