返回作为const引用的参数的问题 [英] Problem with returning arguments that are const references

查看:104
本文介绍了返回作为const引用的参数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道为什么下面的工作不适合,所以我不是问为什么。但我感觉不好,是因为它在我看来是一个非常大的编程障碍。

I know why the following does not work correclty, so I am not asking why. But I am feeling bad about it is that it seems to me that it is a very big programming hindrance.

#include <iostream>
#include <string>
using namespace std;

string ss("hello");

const string& fun(const string& s) {
        return s;
}

int main(){
        const string& s = fun("hello");
        cout<<s<<endl;
        cout<<fun("hello")<<endl;
}

第一个cout不工作。第二个cout将。

The first cout will not work. the second cout will.

我关心的是以下:


不可能想象一种方法实现者想要返回一个是const引用并且不可避免的参数的情况?

我认为这是完全可能的。

C ++在这种情况下?

Is it not possible to imagine a situation where a method implementor wants to return an argument that is a const reference and is unavoidable?
I think it is perfectly possible.
What would you do in C++ in this situation?

推荐答案


$ b

解决方案

我认为这是C ++的一个弱点。有两个因素的不幸结合:

I think it is a slight weakness of C++. There's an unfortunate combination of two factors:


  • 函数的返回只有在其参数为有效时才有效。

  • 隐式转换意味着函数的参数不是可能出现的对象。

  • The function's return is only valid as long as its argument is.
  • Implicit conversion means that the function's argument is not the object it may appear to be.

我没有同情那些没有想到他们有指针/引用的对象的生命周期的人。但隐式转换,这当然是一个具有微妙的优点和缺点的语言特性,不是使分析很容易在这里。有时隐式转换是坏消息,这就是为什么显式关键字存在。但是问题不是转换为 string 一般来说是坏的,这只是对这个函数,以这种不正确的方式使用。

I have no sympathy for people who fail to think about the lifetime of objects they have pointers/references to. But the implicit conversion, which certainly is a language feature with subtle pros and cons, is not making the analysis very easy here. Sometimes implicit conversion is bad news, which why the explicit keyword exists. But the problem isn't that conversion to string is bad in general, it's just bad for this function, used in this incorrect way.

函数的作者可以通过定义一个重载来禁用隐式转换:

The author of the function can in effect disable implicit conversion, by defining an overload:

const char *fun(const char *s) { return s; }

这种改变单独意味着以前是坏的代码,工作。所以我认为在这种情况下这是一个好主意。当然,如果有人定义了 fun 的作者从来没有听说过,并且有一个运算符std :: string的类型()。另外, fun 不是一个现实的函数,对于更有用的例程,你可能不想提供一个等价的操作 char * 。在这种情况下, void fun(const char *); 至少强制调用者显式转换为字符串,这可能有助于他们正确使用函数。

That change alone means the code which previously was bad, works. So I think it's a good idea in this case to do that. Of course it doesn't help if someone defines a type which the author of fun has never heard of, and which has an operator std::string(). Also, fun is not a realistic function, and for more useful routines you might not want to provide an equivalent which operates on char*. In that case, void fun(const char *); at least forces the caller to explicitly cast to string, which might help them use the function correctly.

或者,调用者可以注意到他提供了 char * ,并返回对字符串的引用。在我看来,这是一个免费的午餐,所以警钟应该响,这个字符串来自,以及它将持续多久。

Alternatively, the caller could note that he's providing a char*, and getting back a reference to a string. That appears to me to be a free lunch, so alarm bells should be ringing where this string came from, and how long it's going to last.

这篇关于返回作为const引用的参数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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