move-semantics

Passing two objects, where one holds a reference to another, into a thread

自闭症网瘾萝莉.ら 提交于 2021-02-20 11:51:10
问题 I have two objects where the second one requires the fist one to outlive it because it holds a reference to the first one. I need to move both of them into a thread, but the compiler is complaining that the first one doesn't live long enough. Here is the code: use std::thread; trait Facade: Sync { fn add(&self) -> u32; } struct RoutingNode<'a> { facade: &'a (Facade + 'a), } impl<'a> RoutingNode<'a> { fn new(facade: &'a Facade) -> RoutingNode<'a> { RoutingNode { facade: facade } } } fn main()

why must a Boost.Asio handler be copy constructible?

夙愿已清 提交于 2021-02-18 20:51:52
问题 According to http://www.boost.org/doc/libs/1_61_0/doc/html/boost_asio/reference/Handler.html, a handler provided to io_service::post must be copy constructible. However, this excludes a scenario where a socket is accepted, and the response handler is moved, guaranteeing me that there is only one handler for the job: auto socket = std::make_unique<socket>(); accepter.accept(*socket); service.post([s{std::move(socket)}] { asio::write(*s, buffer("response"), ignored_err); }); So why is this copy

Pushing an object with unique_ptr into vector in C++

一曲冷凌霜 提交于 2021-02-18 10:40:15
问题 I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism. #include <vector> #include <memory> class Transition { public: Transition() {} }; class State { public: State(int num) : num(num), transitions() {} void add_transition(std::unique_ptr<Transition> trans) { transitions.push_back(std:

Pushing an object with unique_ptr into vector in C++

可紊 提交于 2021-02-18 10:40:09
问题 I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism. #include <vector> #include <memory> class Transition { public: Transition() {} }; class State { public: State(int num) : num(num), transitions() {} void add_transition(std::unique_ptr<Transition> trans) { transitions.push_back(std:

What is the default move construct?

老子叫甜甜 提交于 2021-02-17 03:30:36
问题 What is the definition of the default move constructor? I can't think of anything sensible. Maybe a swap on ptr members and copy on values/reference member? 回答1: That's what the standard says (12.8/15): The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. [ Note: brace-or-equal-initializers of non-static data members are ignored. See also the example in 12.6.2. —end note ] The order of initialization is the same as the

c++ : move assignement operator and inheritance

夙愿已清 提交于 2021-02-11 14:17:12
问题 This code compiles and runs fine: #include <iostream> class Base { public: Base(int value) : clean_(true) { value_ = new int; *value_ = value; } ~Base() { if(clean_) delete value_; } Base(Base&& other) noexcept : value_{std::move(other.value_)}, clean_(true) { other.clean_=false; } Base& operator=(Base&& other) noexcept { value_ = std::move(other.value_); other.clean_=false; clean_=true; } void print() { std::cout << value_ << " : " << *value_ << std::endl; } int* value_; bool clean_; };

Should I make my local variables const or movable?

纵然是瞬间 提交于 2021-02-06 15:26:27
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Should I make my local variables const or movable?

我们两清 提交于 2021-02-06 15:25:12
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Should I make my local variables const or movable?

本小妞迷上赌 提交于 2021-02-06 15:24:20
问题 My default behaviour for any objects in local scopes is to make it const . E.g.: auto const cake = bake_cake(arguments); I try to have as little non-functional code as I can as this increases readability (and offers some optimisation opportunities for the compiler). So it is logical to also reflect this in the type system. However, with move semantics, this creates the problem: what if my cake is hard or impossible to copy and I want to pass it out after I'm done with it? E.g.: if (tastes

Should copy assignment operator pass by const reference or by value?

南笙酒味 提交于 2021-02-06 11:15:55
问题 Prior to C++11, it has always been the case that copy assignment operator should always pass by const reference, like so: template <typename T> ArrayStack<T>& operator= (const ArrayStack& other); However, with the introduction of move assignment operators and constructors, it seems that some people are advocating using pass by value for copy assignment instead. A move assignment operator also needs to be added: template <typename T> ArrayStack<T>& operator= (ArrayStack other); ArrayStack<T>&