简体   繁体   中英

Why can't I return a reference from this template function?

I took this example straight out of a book ( Sams Teach Yourself C++ in One Hour a Day ):

// Get the maximum of two values
template <typename objectType>
objectType& GetMax(const objectType& value1, const objectType& value2)
{
    if (value1 > value2)
        return value1;
    else
        return value2;
}

Essentially it is a very verbosely written template function to find the maximum between 2 values of any type.

I attempted to use the function as follows:

// Test the Max function
int x_int = 25;
int y_int = 40;
int max_int = GetMax(x_int, y_int);
cout << "max_int: " << max_int << endl;

double x_double = 1.1;
double y_double = 1.001;
double max_double = GetMax(x_double, y_double);
cout << "max_double: " << max_double << endl;

However, when I attempt to compile and run the code I get the following errors:

Error 1 error C2440: 'return' : cannot convert from 'const int' to 'int &'

Error 2 error C2440: 'return' : cannot convert from 'const int' to 'int &'

Error 3 error C2440: 'return' : cannot convert from 'const double' to 'double &'

Error 4 error C2440: 'return' : cannot convert from 'const double' to 'double &'


If I simply remove the & from the return type of the function it will compile and execute successfully.

Why can't I return a reference from this function? Is the book wrong, or is there something I am missing?

The problem here is the const part of the argument declaration. The variables value1 and value2 are references to constant values, but you return a reference to a non-constant value.

The error explains it all, you can't return a non-const reference to a const reference.

// Get the maximum of two values
template <typename objectType>
const objectType& GetMax(const objectType& value1, const objectType& value2)
{
    if (value1 > value2)
        return value1;
    else
        return value2;
}

You should indicate what lines the errors refer to (as well as post a complete program that others can just copy/paste into their editor).

But I expect them to refer to this line

return value1;

and the similar one after it. The errors tell you exactly what is wrong:

cannot convert from 'const int' to 'int &'

And sure enough, if we investigate the details of what this line is trying to do, we see that the thing you are trying to return is const:

const objectType& value1

but the type the function promises to return is a nonconst refernce:

objectType& GetMax

The return type of the function GetMax is a reference, but you're attempting to return a variable that is a const reference.

That is not allowed and compilation fails.

Attempting to circumvent this mechanism using a const_cast on value1 and value2 is dangerous since the behaviour on casting away const ness from an value that was originally const is undefined.

The obvious thing to do is to change the return type of GetMax to a const reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM