move-semantics

std::forward without perfect forwarding?

元气小坏坏 提交于 2019-12-10 01:24:04
问题 Advice on std::forward is generally limited to the canonical use case of perfectly forwarding function template arguments; some commentators go so far as to say this is the only valid use of std::forward . But consider code like this: // Temporarily holds a value of type T, which may be a reference or ordinary // copyable/movable value. template <typename T> class ValueHolder { public: ValueHolder(T value) : value_(std::forward<T>(value)) { } T Release() { T result = std::forward<T>(value_);

Move semantics and primitive types

左心房为你撑大大i 提交于 2019-12-09 15:34:48
问题 Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; std::cout << "Printing v1" << std::endl; print(v1); std::vector<int> v2(std::make_move_iterator(v1.begin()), std::make_move_iterator(v1.end())); std::cout << "Printing v1" << std::endl; print(v1); std::cout << "Printing v2" << std::endl; print(v2); std::vector<std::string> v3{"some", "stuff", "to", "put", "in", "the", "strings"}; std::cout << "Printing v3" << std::endl; print(v3); std::vector<std::string> v4(std:

Move constructor and initialization list

强颜欢笑 提交于 2019-12-09 11:57:49
问题 I want to implement move constructors (no copy constructor) for a certain type that needs to be a value type in a boost::unordered_map . Let's call this type Composite . Composite has the following signature: struct Base { Base(..stuff, no default ctor) : initialization list {} Base(Base&& other) : initialization list {} } struct Composite { Base member; Composite(..stuff, no default ctor) : member(...) {} Composite(Composite&& other) : member(other.member) {} // <---- I want to make sure

Why would I std::move an std::shared_ptr?

孤者浪人 提交于 2019-12-09 04:00:19
问题 I have been looking through the Clang source code and I found this snippet: void CompilerInstance::setInvocation( std::shared_ptr<CompilerInvocation> Value) { Invocation = std::move(Value); } Why would I want to std::move an std::shared_ptr ? Is there any point transferring ownership on a shared resource? Why wouldn't I just do this instead? void CompilerInstance::setInvocation( std::shared_ptr<CompilerInvocation> Value) { Invocation = Value; } 回答1: I think that the one thing the other

C++: std::move with rvalue reference is not moving contents

不羁岁月 提交于 2019-12-09 00:22:45
问题 Sample program: #include <iostream> #include <string> #include <vector> template <typename T> void print(const T& _vec) { for( auto c: _vec ) std::cout << c << ","; } typedef std::vector<std::string> vecstr_t; struct Trade { explicit Trade(vecstr_t&& vec) : _vec(vec ) { } vecstr_t _vec; }; int main() { vecstr_t tmpV = {"ONE", "TWO", "THREE", "FOUR"}; std::cout << "size 1:" << tmpV.size() << "\t"; print(tmpV); std::cout << "\n" ; Trade t(std::move(tmpV)); std::cout << "size 2:" << tmpV.size()

C++11 / VS2010 : Returning containers of uncopyable but movable objects

Deadly 提交于 2019-12-08 20:16:42
问题 Consider the following code: #include <vector> #include <boost/noncopyable.hpp> struct A : private boost::noncopyable { A(int num, const std::string& name) : num(num), name(name) { } A(A&& other) : num(other.num), name(std::move(other.name)) { } int num; std::string name; }; std::vector<A> getVec() { std::vector<A> vec; vec.emplace_back(A(3, "foo")); // vec.emplace_back(3, "foo"); not available yet in VS10? return vec; // error, copy ctor inaccessible } int main ( int argc, char* argv[] ) { /

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

感情迁移 提交于 2019-12-08 20:14:39
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 discussing(how): Move semantics help facilitate more use of value types. From the original move proposal

What's the intention of forward-by-lvalue-reference constructor while a perfect forwarding constructor exists?

可紊 提交于 2019-12-08 17:38:14
问题 Let's take std::pair<T1, T2> as an example. It has the following two constructors: constexpr pair( const T1& x, const T2& y ); // #1 template< class U1, class U2 > constexpr pair( U1&& x, U2&& y ); // #2 It seems that #2 can handle all cases that #1 can handle (without worse performance), except for cases where an argument is a list-initializer. For example, std::pair<int, int> p({0}, {0}); // ill-formed without #1 So my question is: If #1 is only intended for list-initializer argument, since

Why do not C++11's move constructor/assignment operator act as expected

我的未来我决定 提交于 2019-12-08 17:12:25
问题 #include <iostream> using namespace std; struct A { A() { cout << "A()" << endl; } ~A() { cout << "~A()" << endl; } A(A&&) { cout << "A(A&&)" << endl; } A& operator =(A&&) { cout << "A& operator =(A&&)" << endl; return *this; } }; struct B { // According to the C++11, the move ctor/assignment operator // should be implicitly declared and defined. The move ctor // /assignment operator should implicitly call class A's move // ctor/assignment operator to move member a. A a; }; B f() { B b; //

Zombie objects after std::move

匆匆过客 提交于 2019-12-08 15:40:12
问题 I'm confused about the state of an object after it's been moved using C++0x move semantics. My understanding is that once an object has been moved, it's still a valid object, but its internal state has been altered so that when its destructor is called, no resources are deallocated. But if my understanding is correct, the destructor of a moved object should still be called. But, that doesn't happen when I perform a simple test: struct Foo { Foo() { s = new char[100]; cout << "Constructor