为什么max(max(a,b),c)是错误的 [英] Why max (max(a,b), c) is error

查看:113
本文介绍了为什么max(max(a,b),c)是错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

参考:C ++模板中的代码段:完整指南

Reference: code snippet from C++ Template: The Complete Guide

// maximum of two values of any type (call-by-reference) 
template <typename T> 
inline T const& max (T const& a, T const& b) 
{ 
    return a < b ? b : a; 
} 

// maximum of two C-strings (call-by-value) 
inline char const* max (char const* a, char const* b) 
{ 
    return std::strcmp(a,b) < 0 ? b : a; 
} 

// maximum of three values of any type (call-by-reference) 
template <typename T> 
inline T const& max (T const& a, T const& b, T const& c) 
{ 
    return max (max(a,b), c); // error, if max(a,b) uses call-by-value 
} 

int main () 
{ 
    ::max(7, 42, 68); // OK 

    const char* s1 = "frederic"; 
    const char* s2 = "anica"; 
    const char* s3 = "lucas"; 
    ::max(s1, s2, s3); // ERROR 

} 

上述代码的问题:


问题在于,如果您为三个C字符串调用max(),则
语句

The problem is that if you call max() for three C-strings, the statement

最大返回值(max(a,b),c);变成错误。这是因为对于
C字符串,max(a,b)创建一个新的临时局部值,该局部值可能是函数通过引用返回的

return max (max(a,b), c); becomes an error. This is because for C-strings, max(a,b) creates a new, temporary local value that may be returned by the function by reference.

问题>我仍然不明白以上几点。为什么

Question> I still don't understand the points above. Why


您不能使用三参数版本来计算
三个C字符串的最大值? / p>

"you can't use the three-argument version to compute the maximum of three C-strings"?

//更新

const int* fReturnValue(const int *i)
{
    return i;
}

int main()
{
    int i = 3;
    const int* i4= fReturnValue(&i);

    cout << &i << endl;
    cout << i4 << endl;
}

观察:这两行返回相同的地址。
因此,我假设在fReturnValue函数中,该函数按值返回,但对b / c而言,它并不影响指针地址。换句话说,寄信人地址仍然有效。

Observation: both lines return the same address. So I assume that in function fReturnValue, the function returns by value but it doesn't hurt b/c it is a pointer address. In other words, the return address is still valid.

是真的吗?

推荐答案

问题在于, max C字符串风味按值返回,而不是那么多,它按值接受参数。声明了通用的三向 max 函数以返回引用,但 C字符串 max 返回一个值,这将导致返回对临时文件的引用,该临时文件将在您可以访问之前失效。

The problem is that the C-string flavor of max returns by value, not so much that it takes its arguments by value. The generic 3-way max function is declared to return a reference but the C-string max returns a value, which would then result in returning a reference to a temporary that will be dead before you can access it.

更新 :您添加的新代码与您原来的问题并不等效。这将是:

Update: The new code you added is not equivalent to your original problem. This would:

const int*& fReturnValue(const int *i)
{
    return i;
}

请注意, fReturnValue 返回对局部变量 i 的引用,该变量的生存期在函数返回时结束。因此,该函数返回对无效对象的引用。尝试使用此更改来编译代码,几乎每个编译器都应发出警告。

Note that fReturnValue returns a reference to the local variable i, whose lifetime ends by the time the function returns. So the function returns a reference to an invalid object. Try to compile your code with that change, you should get a warning from pretty much every compiler.

这篇关于为什么max(max(a,b),c)是错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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