move-semantics

Move semantics clarification [duplicate]

对着背影说爱祢 提交于 2019-12-22 18:43:49
问题 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

C++11 - Distinguishing rvalue pointers

不羁岁月 提交于 2019-12-22 18:21:21
问题 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

C++11 Move semantics behaviour specific questions

我是研究僧i 提交于 2019-12-22 16:09:13
问题 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

C++11 Move semantics behaviour specific questions

萝らか妹 提交于 2019-12-22 16:09:06
问题 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

Optimal way to return local value in C++11

人盡茶涼 提交于 2019-12-22 15:25:47
问题 In the old days, if I wanted a string representation of an object A , I would write something with the signature void to_string(const A& a, string& out) to avoid extra copies. Is this still the best practice in C++11, with move semantics and all? I have read several comments on other contexts that suggest relying on RVO and instead writing string to_string(const A& a) . But RVO is not guaranteed to happen! So, how can I, as the programmer of to_string, guarantee the string is not copied

Optimal way to return local value in C++11

流过昼夜 提交于 2019-12-22 15:25:14
问题 In the old days, if I wanted a string representation of an object A , I would write something with the signature void to_string(const A& a, string& out) to avoid extra copies. Is this still the best practice in C++11, with move semantics and all? I have read several comments on other contexts that suggest relying on RVO and instead writing string to_string(const A& a) . But RVO is not guaranteed to happen! So, how can I, as the programmer of to_string, guarantee the string is not copied

How do move semantics work with unique_ptr?

谁说胖子不能爱 提交于 2019-12-22 09:27:56
问题 I was experimenting with using unique_ptr and wrote some simple code to check how it works with move semantics. #include <iostream> #include <vector> using namespace std; class X { public: X(){} ~X() { cout << "Destructor X" << endl; } void Print() { cout << "X" << endl; } }; int main() { unique_ptr<X> ptr(new X()); ptr->Print(); vector<unique_ptr<X>> v; v.push_back(move(ptr)); ptr->Print(); v.front()->Print(); return 0; } The output is as follows: X X X Destructor X My expectation was that

Rcpp and move semantic

被刻印的时光 ゝ 提交于 2019-12-22 08:18:06
问题 I implemented an algorithm in C++ that returns as output a huge array of elements. Now, I would like to implement a wrapper in Rcpp so that I will be able to call this function by using R . I specified in the Makevars file the following setting: PKG_CXXFLAGS = -std=c++11 So that I can use the C++11 version. // [[Rcpp::export]] NumericMatrix compute(int width, int height) { vector<data_t> weights(width * height); compute_weights(weights); NumericMatrix mat(height, width); copy(begin(weights),

c++ deleted move assignment operator compilation issues

落花浮王杯 提交于 2019-12-22 06:59:24
问题 The following code fails with gcc 4.8.0 (mingw-w64) with -O2 -std=c++11 -frtti -fexceptions -mthreads #include <string> class Param { public: Param() : data(new std::string) { } Param(const std::string & other) : data(new std::string(other)) { } Param(const Param & other) : data(new std::string(*other.data)) { } Param & operator=(const Param & other) { *data = *other.data; return *this; } ~Param() { delete data; } Param & operator=(Param &&) = delete; private: std::string * data; }; int main(

How can I take an item from a Vec in Rust?

这一生的挚爱 提交于 2019-12-22 03:46:18
问题 I'm looking for a method that consumes a Vec and returns one element, without the overhead of restoring Vec 's invariants the way remove and swap_remove do: fn take<T>(vec: Vec<T>, index: usize) -> Option<T> However, I can't find such a method. Am I missing something? Is this actually unsafe or impossible? This is a different question from Built in *safe* way to move out of Vec<T>? There the goal was a remove method that didn't panic on out of bounds access and returned a Result . I'm looking