initializer_list和move语义 [英] initializer_list and move semantics

查看:177
本文介绍了initializer_list和move语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我允许将元素移出 std :: initializer_list< T>

#include <initializer_list>
#include <utility>

template<typename T>
void foo(std::initializer_list<T> list)
{
    for (auto it = list.begin(); it != list.end(); ++it)
    {
        bar(std::move(*it));   // kosher?
    }
}

由于 std :: intializer_list< ; T> 需要特殊的编译器注意,并且没有像C ++标准库的正常容器那样的值语义,我宁愿安全而不是问。

Since std::intializer_list<T> requires special compiler attention and does not have value semantics like normal containers of the C++ standard library, I'd rather be safe than sorry and ask.

推荐答案

不,这将无法正常工作;你仍然会得到副本。我对此感到非常惊讶,因为我认为 initializer_list 存在以保持临时数组,直到 move 'd。

No, that won't work as intended; you will still get copies. I'm pretty surprised by this, as I'd thought that initializer_list existed to keep an array of temporaries until they were move'd.

开始结束 initializer_list return const T * ,因此 move 在你的代码中是 T const&&& - 一个不可变的右值引用。这样的表达式不能有意义地移动。它将绑定到 T const& 类型的函数参数,因为rvalues绑定到const lvalue引用,你仍然会看到复制语义。

begin and end for initializer_list return const T *, so the result of move in your code is T const && — an immutable rvalue reference. Such an expression can't meaningfully be moved from. It will bind to an function parameter of type T const & because rvalues do bind to const lvalue references, and you will still see copy semantics.

这可能是因为编译器可以选择使 initializer_list 一个静态初始化的常量,但似乎它会更清洁在编译器的判断下使其类型 initializer_list const initializer_list ,因此用户不知道是否期望一个开始结束的可变结果。

Probably the reason for this is so the compiler can elect to make the initializer_list a statically-initialized constant, but it seems it would be cleaner to make its type initializer_list or const initializer_list at the compiler's discretion, so the user doesn't know whether to expect a const or mutable result from begin and end. But that's just my gut feeling, probably there's a good reason I'm wrong.

更新:我写了 ISO提案,用于 initializer_list 支持的移动类型。这只是一个初稿,它没有在任何地方实现,但你可以看到它的更多的分析的问题。

Update: I've written an ISO proposal for initializer_list support of move-only types. It's only a first draft, and it's not implemented anywhere yet, but you can see it for more analysis of the problem.

这篇关于initializer_list和move语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆