Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 

} 

Stated issues for above code:

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

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

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

// Update

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

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

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

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.

Is that true?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
643 views
Welcome To Ask or Share your Answers For Others

1 Answer

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;
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...