move-semantics

Does an exception use move semantics when thrown in C++11?

瘦欲@ 提交于 2019-12-23 12:22:37
问题 http://www.drdobbs.com/cpp/practical-c-error-handling-in-hybrid-env/197003350?pgno=4 In this article Herb Sutter explains that throwing an exception requires a copy of the exception as it's created as a temporary and therefore uses an std::auto_ptr to get round the copy overhead. In light of move semantics being made available in C++11 is this still necessary? 回答1: I have just checked, and the Standard allows omitting the copy or move of an object specified by the operand of a throw

Why is vector::push_back of local variable not move optimized? [duplicate]

我的未来我决定 提交于 2019-12-23 09:49:52
问题 This question already has answers here : Can compiler generate std::move for a last use of lvalue automatically? (3 answers) Do compilers automatically use move semantics when a movable object is used for the last time? (1 answer) Closed 2 years ago . In C++11 you can use std::vector::push_back in combination with std::move to avoid copies while inserting elements into a vector. Is there a section in the standard that forbids compilers to use std::move automatically with local variables that

Why can't I std::move std::unique_ptrs between std::sets?

…衆ロ難τιáo~ 提交于 2019-12-23 09:16:12
问题 I really want to move some unique_ptr s from one std::set into another: #include <memory> #include <algorithm> #include <set> int main() { std::set<std::unique_ptr<int>> a; std::set<std::unique_ptr<int>> b; a.insert({0, std::unique_ptr<int>(new int(42))}); std::move(a.begin(), a.end(), std::inserter(b, b.end())); } However, my GCC 4.8.5 on CentOS 7 is distinctly unhappy: [root@localhost ~]# g++ test.cpp -std=c++11 -o test In file included from /usr/include/c++/4.8.2/set:60:0, from test.cpp:2:

move ctor of class with a constant data member or a reference member

南楼画角 提交于 2019-12-23 09:04:14
问题 I have some problems understanding when and if the move constructor or move assignment operator are invoked, in particular in the context of a class with constant data member. Consider the class template<typename T> class A { const*T const P ; // constant data member explicit A(const*T p) : P(p) { std::cerr<<" ctor: P="<<P<<'\n'; } void test() const { std::cerr" test: P="<<P<<'\n'; } // move and copy constructors and assignment operators here }; and the test program class B { int X[100]; A<B>

Rationale for std::move_if_noexcept still moving throwing move-only types?

▼魔方 西西 提交于 2019-12-23 08:45:07
问题 move_if_noexcept will: return an rvalue -- facilitating a move -- if the move constructor is noexcept or if there is no copy constructor (move-only type) return an lvalue -- forcing a copy -- otherwise I found this rather surprising, as a move-only type that has a throwing move-ctor will still have this move-ctor invoked by code that uses move_if_noexcept . Has there been given a thorough rationale for this? (Maybe directly or between the lines of N2983?) Wouldn't code be better off not

Why does std::vector use the move constructor although declared as noexcept(false)

早过忘川 提交于 2019-12-23 08:25:09
问题 Wherever I read in the internet, it is strongly adviced that if I want my class to be working well with std::vector (i.e. move semantics from my class were used by std::vector ) I should delcare move constructor as 'noexcept' ( or noexcept(true) ). Why did std::vector use it even though I marked it noexcept(false) as an experiment? #include <iostream> #include <vector> using std::cout; struct T { T() { cout <<"T()\n"; } T(const T&) { cout <<"T(const T&)\n"; } T& operator= (const T&) { cout <<

c++: confusion about forwarding reference

寵の児 提交于 2019-12-23 06:31:07
问题 I read this (incredibly well written) article about Forwarding Reference in C++11 by Scott Meyers. Now, focus on this part of the article: template <class... Args> void emplace_back(Args&&... args); // deduced parameter types ⇒ type deduction; ... // && ≡ universal references So, in contrast with other cases, the ellipses doesn't make the && an rvalue reference, but it's still universal references. From what I've understood, when we have universal references, we can call the function passing

Move and Forward cases use

≯℡__Kan透↙ 提交于 2019-12-23 05:07:49
问题 I followed this tutorial to start to understand the move semantics and rvalue references in C++11. At some point, he implements these two classes with the std::move in the move constructors explaining that we pass the temporary to a move constructor, and it takes on new life in the new scope. In the context where the rvalue expression was evaluated, the temporary object really is over and done with. But in our constructor, the object has a name; it will be alive for the entire duration of our

What's the connection between value semantics and move semantics in C++?

我的未来我决定 提交于 2019-12-23 02:44:17
问题 There're plenty of articles discussing value semantics vs reference semantics, and maybe more trying to explain move semantics. However, No one has ever talked about the connection between value semantics and move semantics . Are they orthogonal concepts? Note: This question is NOT about comparing value semantics vs move semantics, cause it is perfectly clear these two concepts are not "comparable". This question is about how they are connected, specifically (like @StoryTeller said), about

How to implement perfect forwarding on a non-generic type?

不打扰是莪最后的温柔 提交于 2019-12-23 01:24:13
问题 say I have the following code: class Element; typedef shared_ptr<Element> ElementPtr; class Element { public: void add_child(const ElementPtr& elem); private: vector<ElementPtr> children; } inline void Element::add_child(const ElementPtr& elem) { children.push_back(elem); }; And I want to update add_child to use perfect forwarding. I tried changing the function definition (and declaration) so use the following logic: void Element::add_child(ElementPtr&& elem) { children.push_back(forward