C ++视图类型:通过const&还是按价值? [英] C++ view types: pass by const& or by value?

查看:87
本文介绍了C ++视图类型:通过const&还是按价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是最近在代码审查讨论中提出的,但是没有令人满意的结论。所讨论的类型与C ++ string_view TS类似。它们是围绕指针和长度的简单非所有者包装,并装饰有一些自定义函数:

This came up in a code review discussion recently, but without a satisfactory conclusion. The types in question are analogues to the C++ string_view TS. They are simple non-owning wrappers around a pointer and a length, decorated with some custom functions:

#include <cstddef>

class foo_view {
public:
    foo_view(const char* data, std::size_t len)
        : _data(data)
        , _len(len) {
    }

    // member functions related to viewing the 'foo' pointed to by '_data'.

private:
    const char* _data;
    std::size_t _len;
};

提出了一个问题,即是否有一种方法倾向于传递这种视图类型(包括

The question arose as to whether there is an argument either way to prefer to pass such view types (including the upcoming string_view and array_view types) by value or by const reference.

支持按值传递的参数等于较少键入,可以改变本地副本,前提是按值传递或通过const引用。

Arguments in favor of pass by value amounted to 'less typing', 'can mutate the local copy if the view has meaningful mutations', and 'probably no less efficient'.

支持按常量引用传递的参数相当于更惯用传递对象。

Arguments in favor of pass-by-const-reference amounted to 'more idiomatic to pass objects by const&', and 'probably no less efficient'.

是否还有其他考虑因素可能会以一种更好的方式最终使论点摇摆不定?

Are there any additional considerations that might swing the argument conclusively one way or the other in terms of whether it is better to pass idiomatic view types by value or by const reference.

对于这个问题,可以安全地假设C ++ 11或C ++ 14语义以及足够现代的工具链以及目标体系结构等。

For this question it is safe to assume C++11 or C++14 semantics, and sufficiently modern toolchains and target architectures, etc.

推荐答案

如有疑问,请按值传递。

When in doubt, pass by value.

现在,您应该很少有疑问。

Now, you should only rarely be in doubt.

通常价值传递成本很高,几乎没有收益。有时,您实际上希望引用存储在其他位置的可能变异值。通常,在通用代码中,您不知道复制是否是一项昂贵的操作,因此您会犯错。

Often values are expensive to pass and give little benefit. Sometimes you actually want a reference to a possibly mutating value stored elsewhere. Often, in generic code, you don't know if copying is an expensive operation, so you err on the side of not.

应按值传递的原因当有疑问时,是因为价值观更容易推论。当调用函数回调或您拥有的函数时,对外部数据的引用(甚至是 const 一个)可能会在算法中间发生变化,从而呈现出看起来很简单的情况

The reason why you should pass by value when in doubt is because values are easier to reason about. A reference (even a const one) to external data could mutate in the middle of an algorithm when you call a function callback or what have you, rendering what seems to be a simple function into a complex mess.

在这种情况下,您已经有一个隐式引用绑定(绑定到您正在查看的容器的内容)。添加另一个隐式引用绑定(到查看容器的视图对象)也同样不错,因为已经很复杂了。

In this case, you already have an implicit reference bind (to the contents of the container you are viewing). Adding another implicit reference bind (to the view object that looks into the container) is no less bad because there are already complications.

最后,编译器对值的推理要好于他们可以参考值。如果离开本地分析范围(通过函数指针回调),则编译器必须假定const引用中存储的值可能已完全更改(如果不能证明相反)。自动存储中没有人指向该指针的值可以假定没有以类似的方式进行修改-没有定义的方法来访问它并从外部范围进行更改,因此可以假定这种修改不会发生

Finally, compilers can reason about values better than they can about references to values. If you leave the locally analyzed scope (via a function pointer callback), the compiler has to presume the value stored in the const reference may have completely changed (if it cannot prove the contrary). A value in automatic storage with nobody taking a pointer to it can be assumed to not modify in a similar way -- there is no defined way to access it and change it from an external scope, so such modifications can be presumed to not-happen.

当您有机会将值作为值传递时,请拥抱简单性。它很少发生。

Embrace the simplicity when you have an opportunity to pass a value as a value. It only happens rarely.

这篇关于C ++视图类型:通过const&amp;还是按价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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