将移动语义与 std::pair 或 std::tuple 一起使用
假设您想利用移动语义,但其中一个可移动类需要成为 std::pair
的一部分。目的是创建一个返回 std::pair 的函数,该函数可以被视为右值并一起转发。
但我不知道如何做到这一点,除非对 std::pair 本身进行内部更改,以使其了解移动语义。
考虑以下代码:
struct Foo
{
Foo() { }
Foo(Foo&& f) { }
private:
Foo(const Foo& f) { } // do not allow copying
};
int main()
{
Foo f;
std::pair<Foo, int> res = std::make_pair(f, 10); // fails due to private copy constructor
}
问题是 std::make_pair
以及 std::pair
构造函数本身采用两个对象并尝试创建它们的内部副本。这会导致它尝试调用复制构造函数。但在我的示例中,我希望能够将新对移动到res
中,并确保不创建任何副本。我认为这是不可能的,除非 std::pair
本身在内部定义了以下构造函数:
pair(T1&& t1, T2&& t2) : first(std::move(t1)), second(std::move(t2))
但它没有,至少在我正在使用的编译器上没有(gcc 4.3.2 )。可能我的编译器已经过时了,实际上较新的版本将具有此移动感知构造函数。但目前我对移动语义的理解有些不稳定,所以我不确定我是否只是忽略了这里的一些东西。那么,我想要实现的目标是否可能,而无需实际重新实现 std::pair ?或者我的编译器已经过时了?
Suppose you want to take advantage of move semantics, but one of your movable classes needs to be part of an std::pair
. The purpose would be to create a function that returns an std::pair
that can be treated as an rvalue, and forwarded along.
But I can't see how this can be done, unless an internal change to std::pair
itself is made, to make it aware of move semantics.
Consider the following code:
struct Foo
{
Foo() { }
Foo(Foo&& f) { }
private:
Foo(const Foo& f) { } // do not allow copying
};
int main()
{
Foo f;
std::pair<Foo, int> res = std::make_pair(f, 10); // fails due to private copy constructor
}
The problem is that std::make_pair
, as well as the std::pair
constructor itself, takes two objects and tries to make internal copies of them. This causes it to try and invoke the copy constructor. But in my example, I want to be able to move the new pair into res
, and ensure that no copies are made. I would think this wouldn't be possible unless std::pair
itself had the following constructor defined internally:
pair(T1&& t1, T2&& t2) : first(std::move(t1)), second(std::move(t2))
But it doesn't, at least not on the compiler I'm using (gcc 4.3.2). It may be that my compiler is simply out-of-date, and newer versions in fact will have this move-aware constructor. But my understanding of move semantics is somewhat flaky at the moment, so I'm not sure if I'm simply overlooking something here. So, is what I'm trying to accomplish possible, without actually reimplementing std::pair
? Or is my compiler just out of date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但这不是将被调用的
std::pair
构造函数。将调用std::pair
移动构造函数,并且移动构造函数应该完全按照您的预期执行操作 (N3126 20.3.5.2/6):但是,您的示例应该失败,因为在
std::make_pair(f, 10);
中,f
是一个左值,需要显式< code>moved,否则被复制。以下应该有效:That's not the
std::pair
constructor that will be called, though. Thestd::pair
move constructor will be called, and the move constructor should do exactly what you expect (N3126 20.3.5.2/6):However, your example should fail because in
std::make_pair(f, 10);
,f
is an lvalue and needs to be explicitlymove
d, otherwise it is copied. The following should work:GCC 4.3.2 不得有完整的实现。对(和元组)应具有移动构造函数:
(来自 n3126 中的 [pairs.pair])
GCC 4.3.2 must not have a complete implementation. pair (and tuple) shall have move constructors:
(From [pairs.pair] in n3126)