Category : move-semantics

In pubsub google official documentation https://cloud.google.com/pubsub/docs/pull we can find such example. namespace pubsub = ::google::cloud::pubsub; auto sample = [](pubsub::Subscriber subscriber) { return subscriber.Subscribe( [&](pubsub::Message const& m, pubsub::AckHandler h) { std::cout << "Received message " << m << "n"; std::move(h).ack(); PleaseIgnoreThisSimplifiesTestingTheSamples(); }); }; I am curious why std::move is used here. As I understand the only ..

Read more

I am using the CGAL library and writing a function that I hope to use move semantics as described in this question: PointTree preparePointTree(const vector<PlyPoint>& pointCloud) { PointTree tree; kd_tree::prepare<…>(pointCloud, tree); return tree; } and I’m calling it like: PointTree tree = preparePointTree(pointCloud); Unfortunately, it does not compile; GCC complains that: return tree; // <– ..

Read more

I ‘ve wrote such code applying std::move to a prvalue from a temporary constructor. // a std::string instance in class Obj Obj&& myObj1 = std::move(Obj(1,"lost")); print(myObj1); Obj&& myObj2 = Obj(2,"keep"); print(myObj2); And the print effects is like that (print also in constructor and destructor): Construct lost Destroy lost obj1: Construct keep obj2: keep Destroy keep ..

Read more

I’m new with C++11. I wrote this class to help me to well understand move behavior in the classes. class MoveChecker { private: bool wasMoved; int something; public: MoveChecker() : wasMoved(false), something(0) {} MoveChecker(int data) : wasMoved(false), something(data) {} MoveChecker(MoveChecker const& m) : wasMoved(false), something(m.something) {} MoveChecker(MoveChecker&& m) noexcept : wasMoved(true), something(m.something) {} ~MoveChecker() noexcept ..

Read more

Universal references can bind to both rvalue or lvalue references. With std::forward one can perfectly forward arguments, including move references. Is it possible to exclude move semantic? To be more concrete, let me consider this code: #include <iostream> #include <utility> #include <vector> template <class FType, typename T> void f(FType&& g, T&& v) { g(std::forward<T>(v)); } ..

Read more

I’m trying to figure out why this code does not compile? I’ve created user defined destructor, so the move ctor and move assignment operator should not be created. So why complication fails, saying that my class does not fulfill the requirements? #include <iostream> #include <concepts> #include <type_traits> template<class T> requires (!std::movable<T>) struct Singleton { }; ..

Read more

Below is my code #include<iostream> #include<string.h> using namespace std; class mystring { private: char * ptr; public: mystring() { ptr = new char[1]; ptr[0] = ‘{$content}’; } mystring(const char * obj) { cout<<"param called "<<endl; int len = strlen(obj); ptr = new char[len+1]; strcpy(ptr,obj); } mystring(mystring& obj) { cout<<"copy called "<<endl; ptr = new char[strlen(obj.ptr)+1]; ..

Read more

Is this a correct approach to pass std::string parameters to the Impl’s constructor with move semantics? #include <string> #include <memory> using namespace std; class Foo { struct Impl; unique_ptr<Impl> impl_; public: Foo(string s); }; struct Foo::Impl { string s_; Impl(string s) : s_(std::move(s)) {} }; Foo::Foo(string s) : impl_(make_unique<Foo::Impl>(move(s))) { } Or should the Impl’s ..

Read more