move-semantics

Why does std::vector require move-constructors for its elements?

烂漫一生 提交于 2019-12-11 13:51:42
问题 C++98 stated that std::vector elements should have copy-constructors. In C++11 that's no longer the case. Instead, the elements must have move-constructors. Depending on what you do with std::vector, you may or may not really need to call the copy- or move-constructor, yet only one of them is always formally required by the standard. Why? Update: Apparently, the premise is incorrect. My confusion stemmed from reading answers such as this. 回答1: As implied in the comments, there isn't simply

Unexpected destructor call occurs when paired with move constuctor

感情迁移 提交于 2019-12-11 11:38:59
问题 The following code was compiled and run in Visual Studio 2012 Express for Windows Desktop, as a learning exercise. #include <cstdio> class X { public: X() { printf("default constructed\n"); } ~X() { printf("destructed\n");} X(const X&) { printf("copy constructed\n"); } X(X&&) { printf("move constructed\n"); } X & operator= (const X &) { printf("copy assignment operator\n"); } }; X A() { X x; return x; } int main() { { A(); } std::getchar(); } When compiled with compiler optimizations disabled

Returning an rvalue reference from a nonlocal

旧时模样 提交于 2019-12-11 01:21:57
问题 I have a class that is queried for an internal state object: class State {...}; //Has a copy and move constructor class Processor { private: std::unique_ptr<State> state; public: void process(...) { State newState; ... //create this new state state.reset(new State(newState)); } State getState() { return std::move(*state.release()); } }; Is this an appropriate use of std::move ? I can guarantee that getState will only be called once per call to process , but because of the design of this

Copy/move elision versus explicitly deleted copy/move constructors

青春壹個敷衍的年華 提交于 2019-12-11 00:29:56
问题 I want to know when copy/move elision applies (or is allowed to apply) to explicitly delete d copy/move constructors and to non- delete d copy/move constructors. Here are the specifics: Can an explicitly delete d copy ctor or move ctor get elided? Is an attempt to construct an object from another same-type object or temporary object ever allowed to succeed by skipping over the delete d copy ctor and/or delete d move ctor? Here’s what happens in VC12 (with which I’m not sure if there is an

Passing a variable to a function (which alters said variable) repeatedly

倖福魔咒の 提交于 2019-12-11 00:16:20
问题 I'm having a play with Rust and, probably biting off far more than I can chew, am trying to write a module that will encapsulate my database traffic for the rest of the application. The code I'm struggling with is the following: pub fn create_statement(cypher: &str, params: &HashMap<&str, &str>) -> rusted_cypher::Statement { let mut statement = rusted_cypher::Statement::new(cypher); for (field, value) in params.iter() { statement.with_param(field.to_owned(), value.to_owned()); } return

Am I using std::move() too often?

烈酒焚心 提交于 2019-12-10 23:03:31
问题 I finally feel like I understand move semantics in Modern C++, and it's had a dramatic change on the way I write code. Right now, I'm working on an application that uses dependency injection and I'm incorporating my newfound knowledge of move semantics, but I end up using std::move() so much that I'm worried I'm using it incorrectly. Previously, if I wanted to inject a dependency that I needed a copy of in my object, I'd write my constructor like this: class NeedsCopyOfFoo { public:

Should move semantics be used in constructor chains?

为君一笑 提交于 2019-12-10 22:19:46
问题 Suppose I have the following two classes: class Person { public: Person(string name, string surname) : _name(move(name)), _surname(move(surname)) { } ... private: string _name; string _surname; }; class Student : public Person { public: Student(string name, string surname, Schedule schedule) : Person(move(name), move(surname)), _schedule(move(schedule)) { } ... private: Schedule _schedule; }; int main() { Student s("Test", "Subject", Schedule(...)); ... return 0; } Is that a good usage of

Does forbidding copy operations automatically forbid move operations? [duplicate]

只愿长相守 提交于 2019-12-10 19:19:05
问题 This question already has answers here : Default move constructor/assignment and deleted copy constructor/assignment (3 answers) Closed 3 years ago . I want to write a C++ class without any copy and move semantics: I'm just interested in its constructor and destructor. I disabled copy operations (i.e. copy constructor and copy assignment operator) explicitly using C++11's =delete syntax, e.g.: class MyClass { public: MyClass() { /* Init something */ } ~MyClass() { /* Cleanup something */ } //

Should I assign a ref or a copy to a value returning function?

谁说我不能喝 提交于 2019-12-10 18:21:41
问题 We have a bunch of value returning functions: Foo function_1(){ Foo f; // ... return f; } Bar function_2(){ Bar b; // ... return b; } Baz function_3(){ Baz b; // ... return b; } I'm using them to create instantiations of local variables: void example(){ //... const auto foo = function_1(); //... const auto bar = function_2(); //... const auto baz = function_3(); } However, my teammates keep asking me to convert all my instantiations to use & : void example(){ //... const auto& foo = function

When should I use a reference instead of transferring ownership?

自闭症网瘾萝莉.ら 提交于 2019-12-10 18:03:28
问题 From the Rust book's chapter on ownership, non-copyable values can be passed to functions by either transferring ownership or by using a mutable or immutable reference. When you transfer ownership of a value, it can't be used in the original function anymore: you must return it back if you want to. When you pass a reference, you borrow the value and can still use it. I come from languages where values are immutable by default (Haskell, Idris and the like). As such, I'd probably never think