move-semantics

How to actually implement the rule of five?

故事扮演 提交于 2019-12-17 05:40:35
问题 UPDATE at the bottom q1: How would you implement the rule of five for a class that manages rather heavy resources, but of which you want it to be passed around by value because that greatly simplifies and beautifies it's usage? Or are not all five items of the rule even needed? In practice, I'm starting something with 3D imaging where an image is usually 128*128*128 doubles. Being able though to write things like this would make the math alot easier: Data a = MakeData(); Data c = 5 * a + ( 1

Is it possible to std::move objects out of functions? (C++11)

瘦欲@ 提交于 2019-12-17 05:11:26
问题 This program tries to move a string out of a function and use it for the construction of another string: #include <iostream> #include <string> #include <utility> std::string && Get_String(void); int main(){ std::string str{Get_String()}; std::cout << str << std::endl; return 0; } std::string && Get_String(void){ std::string str{"hello world"}; return std::move(str); } The program compiles, but segfaults upon execution. This was my rationale: Get_String will create a local string. A copy of

What is “rvalue reference for *this”?

妖精的绣舞 提交于 2019-12-16 19:04:13
问题 Came across a proposal called "rvalue reference for *this" in clang's C++11 status page. I've read quite a bit about rvalue references and understood them, but I don't think I know about this. I also couldn't find much resources on the web using the terms. There's a link to the proposal paper on the page: N2439 (Extending move semantics to *this), but I'm also not getting much examples from there. What is this feature about? 回答1: First, "ref-qualifiers for *this" is a just a "marketing

How to know or test if a given type is going to be moved

不打扰是莪最后的温柔 提交于 2019-12-13 18:17:44
问题 I'm not looking for a type trait for movable types, nor rules for automatic generation of move operations. What I'm looking for is a general guide to know if a given type is going to be moved or copied, or a way to figure it myself with testing. In some cases, the moving operation is performed without the user ever noticing, for example: void f(std::string) { ... } void f_c(const std::string) { ... } void g() { f(std::string("Hello world!")); // moved, isn't it? f("Hello world!"); // moved,

Copy assignment operator defined in template being deleted by compiler

一世执手 提交于 2019-12-13 16:02:44
问题 I'm familiar with the principle (for example, from this answer and this one) that when a class has a move constructor and/or move assignment operator, its default copy constructor and copy assignment operator are deleted. However, In the examples I've seen, this can be addressed by explicitly defining a new copy constructor and assignment operator. In my particular case, I have a class which is derived by joint inheritance from a C-style struct and a template class. The copy and move

why should all iterators / iterator adaptors not-movable in C++11?

我是研究僧i 提交于 2019-12-13 12:24:21
问题 In this question discussed When to make a type non-movable in C++11 and I discovered Scott Meyers had similar question on comp.std.c++, where SG listed below class types are not movable in C++11 libeary. all mutex types(recursive_mutex , timed_mutex, recursive_timed_mutex, condition_variable type_info error_category locale::facet random_device seed_seq reference_wrapper duration time_point - all iterators / iterator adaptors ios_base basic_istream::sentry basic_ostream::sentry all atomic

Can I write both copy and move assignment operators for a class?

孤街浪徒 提交于 2019-12-13 12:15:56
问题 These are my prototypes, MyClass& operator=(MyClass rhs); // copy assignment MyClass& operator=(MyClass &&rhs); // move assignment But when I call MyClass a, b; a = std::move(b); , there is an error. 556 IntelliSense: more than one operator "=" matches these operands: function "MyClass::operator=(MyClass rhs)" function "MyClass::operator=(MyClass &&rhs)" operand types are: MyClass = MyClass And the compiler returns: Error 56 error C2593: 'operator =' is ambiguous 回答1: Overload resolution is

Why doesn't this RAII move-only type properly emulate `std::unique_ptr`?

僤鯓⒐⒋嵵緔 提交于 2019-12-13 04:49:01
问题 I took the code from this question and edited it to produce a segfault by explicitly calling the destructor of one of the move-constructed objects: using namespace std; struct Foo { Foo() { s = new char[100]; cout << "Constructor called!" << endl; } Foo(const Foo& f) = delete; Foo(Foo&& f) : s{f.s} { cout << "Move ctor called!" << endl; f.s = nullptr; } ~Foo() { cout << "Destructor called!" << endl; cout << "s null? " << (s == nullptr) << endl; delete[] s; // okay if s is NULL } char* s; };

Move semantics and copy constructor

a 夏天 提交于 2019-12-13 04:25:57
问题 I wrote a program as below: #include <iostream> using namespace std; class A { public: A() { } A(A &a) { id = a.id; cout << "copy constructor" << endl; } A& operator=(A &other) { id = other.id; cout << "copy assignment" << endl; return *this; } A(A &&other) { id = other.id; cout << "move constructor" << endl; } A& operator=(A &&other) { id = other.id; cout << "move assignment" << endl; return *this; } public: int id = 10; }; A foo() { A a; return a; } int main() { A a; A a2(a); // output:

What is the “best” way to overload arithmetic operators in modern C++?

会有一股神秘感。 提交于 2019-12-13 02:34:26
问题 I want to implement a kind of "number-like" mathematical objects (say, elements in a group or a ring) in C++. I'm sure that I'm not the only one dealing with this problem, so there may be abundant amount of discussions about what is the "best" way of overloading arithmetic operators. However, I couldn't find satisfactory answers (although maybe I was too lazy to googling more). Let's say I want to overload the operator "+" for a class A. The problem is, there are too many different overloads