move-semantics

Move Semantics for POD-ish types

守給你的承諾、 提交于 2019-12-05 20:28:36
问题 Is there any point implementing a move constructor and move assignment operator for a struct or class that contains only primitive types? For instance, struct Foo { float x; float y; float z; /// ... ctor, copy ctor, assignment overload, etc... }; I can see that, if I had something more complex, like: struct Bar { float x,y,z; std::string Name; }; where I'd rather move Name than copy it, a move ctor would make sense. However, "moving" a float doesn't (semantically) make sense to me. Thoughts?

Is it necessary to define move constructors from different classes?

大兔子大兔子 提交于 2019-12-05 19:11:23
问题 Consider the following: struct X { Y y_; X(const Y & y) :y_(y) {} X(Y && y) :y_(std::move(y)) {} }; Is it necessary to define a constructor like the second one in order to take full advantage of move semantics? Or will it be taken care of automatically in the appropriate situations? 回答1: Yes, but no. Your code should just be this: struct X { Y y_; X(Y y) : // either copy, move, or elide a Y y_(std::move(y)) // and move it to the member {} }; If you ever say in design "I need my own copy of

returning a string from a function

旧街凉风 提交于 2019-12-05 18:52:59
I wanted to write a function that'll be cross platform (win32 & linux), and return a string representation of the datetime [hh:mm:ss dd-mm-yyyy]. Knowing that I just want to use the returned string as a temporary in a stream fashion as below: std::cout << DateTime() << std::endl; I considered writing a function with the following prototype const char* DateTime(); If you return a character array, you must delete it once you're done. But I just want a temporary, I don't want to have to worry about de-allocating the string. So I've written a function that just returns an std::string: #include

Rcpp and move semantic

杀马特。学长 韩版系。学妹 提交于 2019-12-05 16:49:31
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), end(weights), mat.begin()); return mat; } The above wrapper function remains efficient if the

Why can't I pass an rvalue std::stringstream by value to a function?

左心房为你撑大大i 提交于 2019-12-05 12:05:40
Why does this code not compile? #include <sstream> void f(std::stringstream) { } int main() { f(std::stringstream{}); } I get this error: error: use of deleted function ‘std::basic_stringstream<char>::basic_stringstream(const std::basic_stringstream<char>&)’ f(std::stringstream{}); ^ If I replace std::stringstream with another type that's noncopyable it works fine. Shouldn't this use stringstream 's move constructor? The missing move constructor for stringstream is a known missing feature in GCC's implementation of the C++ standard library. If I'm reading the comments on that report correctly,

c++ deleted move assignment operator compilation issues

我的未来我决定 提交于 2019-12-05 10:58:09
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() { Param param; param = Param("hop"); return 0; } With the error : error: use of deleted function

What kinds of types does qsort not work for in C++?

ぐ巨炮叔叔 提交于 2019-12-05 10:47:43
std::sort swaps elements by using std::swap , which in turn uses the copy constructor and assignment operators, guaranteeing that you get correct semantics when exchanging the values. qsort swaps elements by simply swapping the elements' underlying bits, ignoring any semantics associated with the types you are swapping. Even though qsort is ignorant of the semantics of the types you are sorting, it still works remarkably well with non-trivial types. If I'm not mistaken, it will work with all standard containers, despite them not being POD types. I suppose that the prerequisite for qsort

Lvalue reference constructor is called instead of rvalue reference constructor

大憨熊 提交于 2019-12-05 10:41:41
There is this code: #include <iostream> class F { public: F() = default; F(F&&) { std::cout << "F(F&&)" << std::endl; } F(F&) { std::cout << "F(F&)" << std::endl; } }; class G { F f_; public: G(F&& f) : f_(f) { std::cout << "G()" << std::endl; } }; int main(){ G g = F(); return 0; } The output is: F(F&) G() Why F(F&) constructor is called instead of F(F&&) constructor in constructor of class G ? The parameter for constructor of class G is F&& f which is rvalue reference but constructor for lvalue reference is called. Why F(F&) constructor is called instead of F(F&&) constructor in constructor

Is the capacity required to be preserved when moving a std::vector?

孤人 提交于 2019-12-05 10:31:25
问题 Consider the following code: std::vector vec; vec.reserve(500); size_t cap = vec.capacity(); std::vector newVec = std::move(vec); assert(cap == newVec.capacity()); In pretty much any implementation you run across, this will work. I don't care about what implementations do. I want to know what the standard requires . Will the moved-to vector have the same capacity as the original? Or will the assert trigger? 回答1: Looking at the standard, it appears that nothing is required from the move

Why is `T(const T&&)` called a move constructor?

无人久伴 提交于 2019-12-05 08:04:19
[C++11: 12.8/3]: A non-template constructor for class X is a move constructor if its first parameter is of typeX&& , const X&& , volatile X&& , or const volatile X&& , and either there are no other parameters or else all other parameters have default arguments (8.3.6). [..] Why is a constructor that takes a const rvalue reference called a "move constructor" by the standard? Surely it's self-evident that this prohibits meaningful move semantics in all but the most fringey cases? "According to me", as the SO saying goes, T(const T&&) shouldn't be deemed a "move constructor" as such, since it's