F
Frederick Gotham
We all know that there's special rules for the binding of a "reference to
const" to an R-value, i.e.:
int const &i = 5;
acts as if it were:
int const __rval = 5;
int const &i = __rval;
Consider the following function:
int const &Func()
{
int i = 7;
return i;
}
We see that the reference is bound to an L-value in the return statement,
and so the "bind reference to const to R-value" rules don't apply. Must the
return statement be rewritten as the following?
return (int)i;
in order to enforce the rules for binding a "reference to const" to an R-
value? Or is the function just destined to fail?
Here's a program to demonstrate:
#include <iostream>
#include <ostream>
using std::cout;
using std::endl;
int const &Func()
{
int i = 7;
return (int)i;
}
int main()
{
cout << Func() << endl;
}
const" to an R-value, i.e.:
int const &i = 5;
acts as if it were:
int const __rval = 5;
int const &i = __rval;
Consider the following function:
int const &Func()
{
int i = 7;
return i;
}
We see that the reference is bound to an L-value in the return statement,
and so the "bind reference to const to R-value" rules don't apply. Must the
return statement be rewritten as the following?
return (int)i;
in order to enforce the rules for binding a "reference to const" to an R-
value? Or is the function just destined to fail?
Here's a program to demonstrate:
#include <iostream>
#include <ostream>
using std::cout;
using std::endl;
int const &Func()
{
int i = 7;
return (int)i;
}
int main()
{
cout << Func() << endl;
}