move-semantics

Move constructor for derived class

蹲街弑〆低调 提交于 2019-12-01 06:19:59
I have 2 classes: template<typename T> class base{ T t; public: base(base &&b): t(std::move(b.t)){} }; template<typename T, typename T2> class derived : protected base<T>{ T2 t2; public: derived(derived &&d): base<T>(std::move(d)), t2(std::move(d.t2)){} }; I move entire d object in the derived move-constructor to initialize base part and d becomes invalid but I still need it to use it's part for t2 initialization Is it possible to do such a thing? I would say that your construct is correct except for a little syntax error, you need to qualify base<T> in the initializer list: derived(derived &

Move constructor for derived class

穿精又带淫゛_ 提交于 2019-12-01 04:16:36
问题 I have 2 classes: template<typename T> class base{ T t; public: base(base &&b): t(std::move(b.t)){} }; template<typename T, typename T2> class derived : protected base<T>{ T2 t2; public: derived(derived &&d): base<T>(std::move(d)), t2(std::move(d.t2)){} }; I move entire d object in the derived move-constructor to initialize base part and d becomes invalid but I still need it to use it's part for t2 initialization Is it possible to do such a thing? 回答1: I would say that your construct is

Move constructor for std::string from char*

Deadly 提交于 2019-12-01 04:10:26
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. std::string will make a copy of the null terminated string argument and manage that copy. There's no way to have it take ownership of a string you pass to it. So what you're doing is correct, the only improvement I'd suggest is

Unnecessary emptying of moved-from std::string

假如想象 提交于 2019-12-01 03:47:59
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, __str._M_local_buf, _S_local_capacity + 1); else { _M_data(__str._M_data()); _M_capacity(__str._M

initializer_list immutable nature leads to excessive copying

时光总嘲笑我的痴心妄想 提交于 2019-12-01 03:04:57
Why does the access to std::initializer_list not allow us to change its content? It's a big disadvantage of std::initializer_list when using it for its main purpose (to initialize a container), since it's use leads to excessive copy-construction/copy-assignment, instead of move-construction/move-assignment. #include <initializer_list> #include <iostream> #include <vector> #include <cstdlib> struct A { A() = default; A(A const &) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A(A &&) { std::cout << __PRETTY_FUNCTION__ << std::endl; } A & operator = (A const &) { std::cout << __PRETTY

Why are move semantics for a class containing a std::stringstream causing compiler errors?

▼魔方 西西 提交于 2019-12-01 02:43:36
How can I make this simple class movable? What I thought was correct just produces a wall of errors... #include <iostream> #include <sstream> #include <utility> class message { public: message() = default; // Move constructor message( message &&other ) : stream_( std::move( other.stream_ ) ) // Nope {} // Move assignment message &operator=( message &&other ) { if ( this != &other ) { stream_ = std::move( other.stream_ ); // Nope #2 } return *this; } private: message( const message & ) = delete; message &operator=( const message & ) = delete; std::stringstream stream_; // Other member variables

Is it possible to move a boost::optional?

拈花ヽ惹草 提交于 2019-12-01 02:42:22
I've been trying to define a defaulted move constructor in a class with a boost::optional member variable. #include <boost/optional.hpp> #include <utility> #include <vector> struct bar {std::vector<int> vec;}; struct foo { foo() = default; foo(foo&&) = default; boost::optional<bar> hello; }; int main() { foo a; foo b(std::move(a)); } My compiler supports both move semantics and defaulted move constructors, but I cannot get this to work. % clang++ foo.cc -std=c++11 -stdlib=libc++ foo.cc:15:7: error: call to deleted constructor of 'foo' foo b(std::move(a)); ^ ~~~~~~~~~~~~ foo.cc:9:3: note:

Warn if accessing moved object in C++11 [duplicate]

随声附和 提交于 2019-12-01 02:30:36
Possible Duplicate: What can I do with a moved-from object? After you called std::move and passed the result to a function, you generally have to assume that accessing the moved object later will result in undefined behavior. Are there tools that can detect those accesses and warn you. For example: { Widget w; foo(std::move(w)); // w may be undefined at this point w.doSomething(); // WARN } At least, gcc 4.7.2 and clang 3.2 with -Wall do not complain. Update: Looking back at this question, the critical point is that the compiler cannot decide whether an object is still valid after it has been

Move which throws?

可紊 提交于 2019-12-01 02:24:26
问题 To my understanding, move-constructors and move-assign must be marked noexcept in order for the compiler to utilize them when, for example, reallocating inside a vector. However, is there any real-world case where a move-assign, move-construct might actually throw? Update : Classes that for example has an allocated resource when constructed cant be no-throw move. 回答1: However, is there any real-world case where a move-assign, move-construct (or swap) might actually throw? Yes. Consider an

Can I move-assign a std::map's contents into another std::map?

﹥>﹥吖頭↗ 提交于 2019-12-01 02:09:27
Is it possible to insert the contents of a temporary std::map temp into another std::map m by using move semantics, such that the values from the temporary are not copied and are reused? Let's say one has: std::map<int, Data> temp; std::map<int, Data> m; One way of copying values from temp into m is: m.insert(temp.begin(),temp.end()); How can I move the temp elements into m , instead of copying? HINT: Read the update first! The current C++11 standard and the C++14 draft do not provide a member function to enable this feature. As lavr suggested you can still write m.insert(make_move_iterator