Binding reference to an R-value

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

AnonMail2005

Frederick said:
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;
}
In order to return a reference from Func, you must return a reference
to something that exists after Func returns. Variables defined on
the stack do not satisfy this requirement.

One example which is the classic Meyer's singleton:

int const & Func ()
{
static int i = 7;
return i;
}

There are others, but I don't know what your requirements are.
 
F

Frederick Gotham

(e-mail address removed) posted:
In order to return a reference from Func, you must return a reference
to something that exists after Func returns.

Acknowledged.


Variables defined on the stack do not satisfy this requirement.


Acknowledged.

One example which is the classic Meyer's singleton:

int const & Func ()
{
static int i = 7;
return i;
}

There are others, but I don't know what your requirements are.


The code in my original post explained.
 
K

Kai-Uwe Bux

Frederick said:
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?
[snip]

I think

return int(i);

should be fine. As far as I can see, int(i) will construct a temporary int
r-value that binds nicely to the const int &. Since I have no idea what
kind of black magic a C-style cast performs, I cannot comment on

return (int)i;


Best

Kai-Uwe Bux
 
A

Alf P. Steinbach

* Kai-Uwe Bux:
I think

return int(i);

should be fine. As far as I can see, int(i) will construct a temporary int
r-value that binds nicely to the const int &.

Unfortunately that temporary ceases to exist immediately afterwards,
since execution leaves the scope where it's created, which yields UB.
 
K

Kai-Uwe Bux

Alf said:
* Kai-Uwe Bux:

Unfortunately that temporary ceases to exist immediately afterwards,
since execution leaves the scope where it's created, which yields UB.

Oops, you are right.


Thanks

Kai-Uwe Bux
 
F

Frederick Gotham

Kai-Uwe Bux posted:
Since I have no idea what kind of black magic a C-style cast performs, I
cannot comment on

return (int)i;


int(i) and (int)i are exactly equivalent in all contexts, with all types.
 
F

Frederick Gotham

Kai-Uwe Bux posted:
Oops, you are right.


I'm not so sure about that.

The reference which is returned by the function should have scope _outside_
of the function, should it not?
 
A

Alf P. Steinbach

* Frederick Gotham:
Kai-Uwe Bux posted:



I'm not so sure about that.

The reference which is returned by the function should have scope _outside_
of the function, should it not?

Nope. Or rather yes, if by scope you mean lifetime, but the language
doesn't do that for you.
 
K

Kai-Uwe Bux

Frederick said:
Kai-Uwe Bux posted:



I'm not so sure about that.

The reference which is returned by the function should have scope
_outside_ of the function, should it not?

I don't think so, but I might be off. Here is what I found in the standard
[12.2/5]:

... A temporary bound to the returned value in a function return statement
(6.6.3) persists until the function exits. ...


So even if the temporary exists outside the function, it won't exist very
long.


Best

Kai-Uwe Bux
 
O

Old Wolf

Frederick said:
Kai-Uwe Bux posted:


int(i) and (int)i are exactly equivalent in all contexts, with all types.

Except the following contexts (among others):

#include <iostream>
int i = 5;

int main()
{
int(i);
std::cout << "i = " << i << "\n";
}

int *fred2()
{
return new int(i);
}
 
F

Frederick Gotham

Old Wolf posted:
types.

Except the following contexts (among others):
new int(i)


Yes. In the context of casting though, they're always equivalent.

When using new, it's an implicit conversion rather than a hardcore cast, as
demonstrated by the type mismatch in the following:

int main()
{
double *p = 0;

new int(p);
}

While we're playing grammar games, you could have also said:

sizeof (int)i

Vs.

sizeof int(i)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,802
Messages
2,569,663
Members
45,433
Latest member
andrewartemow

Latest Threads

Top