473,395 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Binding reference to an R-value


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

--

Frederick Gotham
Oct 14 '06 #1
11 2031
Frederick Gotham wrote:
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;
}

--

Frederick Gotham
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.

Oct 14 '06 #2
An**********@gmail.com 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.
--

Frederick Gotham
Oct 14 '06 #3
Frederick Gotham wrote:
>
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
Oct 15 '06 #4
* Kai-Uwe Bux:
Frederick Gotham wrote:
>>
int const &Func()
{
int i = 7;

return i;
}

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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 15 '06 #5
Alf P. Steinbach wrote:
* Kai-Uwe Bux:
>Frederick Gotham wrote:
>>>
int const &Func()
{
int i = 7;

return i;
}

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.
Oops, you are right.
Thanks

Kai-Uwe Bux
Oct 15 '06 #6
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.

--

Frederick Gotham
Oct 15 '06 #7
Kai-Uwe Bux posted:
>Unfortunately that temporary ceases to exist immediately afterwards,
since execution leaves the scope where it's created, which yields UB.

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?
--

Frederick Gotham
Oct 15 '06 #8
* Frederick Gotham:
Kai-Uwe Bux posted:
>>Unfortunately that temporary ceases to exist immediately afterwards,
since execution leaves the scope where it's created, which yields UB.
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?
Nope. Or rather yes, if by scope you mean lifetime, but the language
doesn't do that for you.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 15 '06 #9
Frederick Gotham wrote:
Kai-Uwe Bux posted:
>>Unfortunately that temporary ceases to exist immediately afterwards,
since execution leaves the scope where it's created, which yields UB.

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?
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
Oct 15 '06 #10
Frederick Gotham wrote:
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.
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);
}

Oct 15 '06 #11
Old Wolf posted:
Frederick Gotham wrote:
>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.
>
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)

--

Frederick Gotham
Oct 16 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: festiv | last post by:
Hi there, I want to learn how the compiler is implementing the dynamic binding. where can i read about this subject (the hole process). thanks.
1
by: JD Kronicz | last post by:
Hi .. I have an issue I have been beating my head against the wall on for some time. I am trying to use late binding for MS graph so that my end users don't have to worry about having the right...
7
by: PC Datasheet | last post by:
I have this: Dim Cbr As Object Dim Ctl As Object Set Cbr = Application.Commandbars("CourseCalendarMonth") Set Ctl = Application.Commandbars("CourseCalendarMonth").Controls("CalendarMonth") Is...
14
by: Composer | last post by:
I've read many postings about the problem of Access.References.IsBroken and the consensus seems to be that late binding is the cure-all. I have a very complex Access application that needs...
9
by: Zlatko Matić | last post by:
I was reading about late binding, but I'm not completely sure what is to be done in order to adjust code to late binding... For example, I'm not sure if this is correct: early binding: Dim ws...
5
by: Daniel Bass | last post by:
..Net is great for modulerising libraries, so that all you need do to access a DLL, is simply call Add Reference and wallah, it's as though the library were written in your project. But what...
10
by: Fares Eidi | last post by:
I am a VB6 intermediate programmer just starting out using VB.net and would like to ask what I'd be missing out on if I just use time saving techniques like data binding, data adapter wizards etc...
2
by: Randy | last post by:
Hi, I'm trying to figure out some binding navigator code. If I have the primary field value as selected by the user in a combo box, how can I instruct the binding navigator to move to that...
3
ADezii
by: ADezii | last post by:
The process of verifying that an Object exists and that a specified Property or Method is valid is called Binding. There are two times when this verification process can take place: during compile...
14
by: Siv | last post by:
hi, I am converting an application that writes to an Excel spreadsheet and the code trips the "option Strict" that I would like on because the parser says "option Strict On disallows late...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.