当一个函数应该返回一个RValue引用时是否有一个原因? 技巧,技巧,成语或模式? MyClass&& func( … ); 我知道一般返回引用的危险,但有时候我们也是这么做的,难道我们( T& T::operator=(T)只是一个习惯用法的例子)。 但是T&& func(…)怎么样? 我们可以从中获益吗? 当你编写库或API代码时,与仅仅客户端代码相比,可能会有所不同?
我一直在试图围绕C ++ 11中的移动语义如何工作,而且我在理解移动对象需要满足的条件方面遇到了很多麻烦。 看看这里的答案并不能真正解决我的问题,因为看不到我们的问题,因为我们无法看出如何以合理的方式将它应用于pimpl对象,尽pipe移动语义对于pimpls来说是完美的 。 我的问题的最简单的例子涉及到pimpl的成语,就像这样: class Foo { std::unique_ptr<FooImpl> impl_; public: // Inlining FooImpl's constructors for brevity's sake; otherwise it // defeats the point. Foo() : impl_(new FooImpl()) {} Foo(const Foo & rhs) : impl_(new FooImpl(*rhs.impl_)) {} Foo(Foo && rhs) : impl_(std::move(rhs.impl_)) {} Foo & operator=(Foo rhs) { std::swap(impl_, rhs.impl_); return *this; } void […]
C ++ 11 std::move(x)函数根本就不移动任何东西。 这只是一个演员r值。 为什么这样做? 这不是误导?