STL容器的右值ref限定词 [英] rvalue ref-qualifiers for STL containers

查看:97
本文介绍了STL容器的右值ref限定词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要使用STL容器的元素访问成员功能,例如 std :: array :: operator [] std :: vector :: operator [] 没有右值引用限定符重载?当然我可以做 std :: move(generate_vector()[10]),但是我很好奇在标准化ref限定符时是否考虑添加右值ref限定符重载.

Why element access member functions of STL containers, e.g. std::array::operator[] or std::vector::operator[] do not have rvalue ref-qualifier overloads? Of course I can do std::move(generate_vector()[10]), but I'm curious if adding rvalue ref-qualifier overloads was considered when ref-qualifiers were being standardized.

我认为 std :: array< T,N> std :: tuple< T,T,...,T> 确实是同一回事,对于常量与非常量以及左值与右值的所有组合,后者的元素访问函数(即 std :: get )"已重载.为什么不选择前者呢?

I think std::array<T, N> and std::tuple<T, T, ..., T> are really the same thing, and the "element access function (i.e., std::get)" of the latter is overloaded for all combinations of const vs non-const and lvalue vs rvalue. Why not the former?

将rvalue ref限定的元素访问成员函数(返回rvalue引用)添加到我的自定义容器中是个好主意吗?

Is it a good idea to add rvalue ref-qualified element access member functions (which return rvalue references) to my custom container?

Richard Critten的评论.我认为这偶尔会有用.

To Richard Critten's comment. I think this can be occasionally useful.

例如,您有一个函数返回一个在该函数内部构造的容器,但是您可能只对该容器的第一个元素感兴趣.是的,这很愚蠢.在这种情况下,最好使用只构造第一个元素的简单函数.但是,如果该功能不是您的功能,那么您别无选择.

For example, you have a function that returns a container constructed inside that function, but you may be interested only to the first element of that container. Yes, that's silly. In that case, it's definitely better to use simpler function that only constructs the first element. But if the function is not yours, you have no such a choice.

或者,可能会有更多一般示例.您有一个构造容器的函数,并且希望对该容器进行处理以获得另一个结果.例如,您可能希望对该容器执行 std :: reduce std :: unique_copy .(似乎可以防止在执行 std :: reduce 时修改元素,但是让我们假设我们已经实现了允许修改的元素.)在这种情况下,可以使用 std:: make_move_iterator ,但是为什么不让容器本身返回移动迭代器呢?

Or, there may be more general examples. You have a function that constructs a container, and you wish to process that container to get another result. For example, you may wish to perform std::reduce, or std::unique_copy to that container. (It seems that it is prevented to modify elements during performing std::reduce, but let us just assume we have implemented our own that allows modifications.) In this case, one may use std::make_move_iterator, but why not let the container itself returns move iterators?

实际上,当我在容器类中实现一些视图"类时遇到了这个问题.似乎都需要可变视图(左值引用),不可变视图(常量参考)和可移动视图(右值引用),我必须确定从可移动视图类的元素访问成员函数中返回什么:左值或右值引用?将右值引用返回到容器本身不公开此类接口的元素,这让我感到有些奇怪.哪个是正确的?

In fact I encountered this problem when I'm implementing some "view" classes to a container class. Mutable view (lvalue reference), immutable view (const reference), and movable view (rvalue reference) seem to be all needed, and I have to determine what to return from element access member functions of the movable view class: lvalue or rvalue references? It felt a little bit weird to me, to return rvalue references to elements where the container itself does not expose such interfaces. Which one is correct?

  1. 左值引用.
  2. 右值引用.
  3. 可移动视图通常是不正确的.这种事情不应该经常使用,在我的设计中应该有一些严重的问题.

推荐答案

向返回引用的所有内容添加ref-qualifier并没有什么特别的问题,但是基本上使成员数量加倍,通常具有相同的实现除了将返回结果包装在 std :: move 中.

There isn't a particular problem with adding ref-qualifiers to everything that returns references, however that basically doubles the number of members, which will generally have identical implementations apart from wrapping the return in std::move.

class A
{
    int a;

    int& operator[](std::size_t pos) & { return a; }
    int&& operator[](std::size_t pos) && { return std::move(a); }
};

标准库拒绝提供这些重载,就像拒绝提供许多 volatile 重载一样.在这种情况下,您可以在需要的地方 std :: move & 值.

The standard library has declined to provide these overloads, in the same way that it has declined to provide many volatile overloads. In this case, you can just std::move the & value, where you need it.

如果您正在编写自己的容器,则没有安全性来避免此类重载.它确实增加了维护负担,所以我建议不要这样做.

If you are writing your own containers, then there is no safeness reason to avoid such overloads. It does increase the maintenance burden, so I would advise against it.

这篇关于STL容器的右值ref限定词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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