有没有办法在函数调用中使按引用传递和按值传递显式? [英] Is there a way to make passing by reference, and passing by value explicit in the function call?

查看:66
本文介绍了有没有办法在函数调用中使按引用传递和按值传递显式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您要看这段代码,

int x = 0;
function(x);
std::cout << x << '\n';

您将无法通过任何语法手段来验证参数x是通过引用传递还是通过值传递.您肯定会知道的唯一方法是,您查看的是函数声明还是函数定义.

you would not be able to verify through any means of syntax, that parameter x is being passed by reference or that it's being passed by value. The only way you would know for sure, is if you looked at either the function declaration or function definition.

以下是我认为这可能是一个问题的简单示例:

Here is a simple example of how I believe this could be a problem:

std::string Lowercase(std::string str); //<- this is hidden away in code; probably in a different file.

int main(){
    std::string str = "HELLO";
    Lowercase(str);
    std::cout << str << '\n'; //<- Bug! we expected to output "hello".  The problem is not very easy to spot, especially when a function name sounds as though it will change the passed in value.
}

为了避免为了理解函数行为而不得不在函数调用和函数声明(或在某些情况下,为文档)之间跳转,有一种方法可以显式地记录函数调用的语法,即该参数是预期更改(即参考参数)还是正在发送副本(即按值传递)?

In order to avoid having to jump between the function call and the function declaration (or in some cases, the documentation) in order to understand the function behavior, is there a way to explicitly document in the syntax of a function call that the parameter is expected to change (i.e. a reference parameter) or that a copy is being sent (i.e. pass by value)?

我意识到,也可以通过const&与按值传递的概念类似,因为传入的变量在函数调用后不会更改其值.

I realize that there is also the option of passing by const& which has the similar notion to passing by value, in that the variable passed in, will not have its value changed after the function call.

我敢肯定,语言中的各种情况都可能会加深理解如何传递参数的复杂性- 但是我很好奇,有没有办法以我想要的方式解决这个问题?

I'm sure there are all kinds of situations in the language that might add to the complexity of understanding how a parameter is being passed- but I'm curious, is there a way to combat this problem in the way I want to?

我注意到有些人编写了两个类似的函数.其中一个采用值参数,另一个采用指针.这样就可以调用这样的函数:

I've noticed that some people write two similar functions. One of them takes a value parameter, the other one takes a pointer. That allows calling a function like this:

Lowercase(str); //we assume the value will not change
Lowercase(&str); //we assume the value will change

但是此解决方案还有许多其他问题,我不想失去引用的好处.另外,我们仍在对该行为进行假设.

But this solution has many other issues, and I would not like to lose the benefit of references. Plus, we are still making assumptions on the behavior.

推荐答案

有人坚持认为传递可变对象的正确方法是使用指针.也就是说,您会通过

Some people insist that the correct way to pass mutable object is to use a pointer. That is, you would pass

Lowercase(&str);

显然,

...和Lowercase()将被实现为采用指针.这种方法可能适合您的需求.

... and Lowercase() would, obviously, be implemented to take a pointer. That approach may suit your needs.

但是我想提一提,这不是我要做的!相反,我赞成的方法是改用适当的名称.例如,

I want to mention, however, that this is not what I would do! Instead, the approach I favor is to use appropriate names instead. For example,

inplace_lowercase(str);

几乎说了这是怎么做的.显然,inplace_lowercase()实际上是一种算法,并且具有一点魔力可以合理地称为

pretty much says what it is going to do. Clearly, inplace_lowercase() would actually be an algorithm and with a bit of magic could be reasonably be called as

inplace_lowercase(str.begin() + 1, str.end());

也是.

以下是我不喜欢通过指针传递参数和/或为什么我不相信显式指示如何传递参数的一些原因:

Here are a few reasons why I don't like passing arguments by pointer and/or why I don't believe in an explicit indication of how the argument is passed:

  • 指针可以为空.我认为必须使用参考参数作为参考.
  • 通过指针传递仍然不会指示是否可以修改参数,因为参数可能是T const*.
  • 拥有有意义的名称实际上使从一开始就更容易理解发生了什么.
  • 在未查阅文档说明和/或不知道被调用函数将要做什么的情况下进行调用无论如何都无法正常工作,并且指出如何传递正在试图解决更深层问题的症状.
  • Pointers can be null. A mandated reference parameters should, in my opinion, be a reference.
  • Passing by pointer still doesn't indicate whether the argument may be modified are not as the argument may be a T const*.
  • Having meaningful names makes it actually easier to understand what's going on in the first place.
  • Calling something without consulting its documentation and/or knowing what the called function will do doesn't work anyway and indicating how things are passed is trying to cure symptoms of a deeper problem.

这篇关于有没有办法在函数调用中使按引用传递和按值传递显式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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