References discussion

J

jacob navia

I would like to thank all people that participated in that discussion.

I have learned interesting points that I did not even consider before
(thanks to Shao for pointing out that references could bypass the
"array decay" problem and pass size information into the called
function)

I discovered quite a few bugs in my implementation, that I am correcting
now.

Ian discovered that there was an incompatibility between my
implementation and the C++ behavior. I build a constant reference
object without *requiring* that the reference is declared as "const"...

I do not see really the problem but will change this to remain
compatible with C++.

I will post a more formal description of my references implementation
when I have ironed out all the bugs.

Thanks again

jacob
 
I

Ian Collins

I would like to thank all people that participated in that discussion.

I have learned interesting points that I did not even consider before
(thanks to Shao for pointing out that references could bypass the
"array decay" problem and pass size information into the called
function)

I discovered quite a few bugs in my implementation, that I am correcting
now.

Ian discovered that there was an incompatibility between my
implementation and the C++ behavior. I build a constant reference
object without *requiring* that the reference is declared as "const"...

I do not see really the problem but will change this to remain
compatible with C++.

From Tim Rentsch's post, something like

void foo( int& x )
{
++x;
}

int n = 42;

foo( n );

foo( n++ ); // What is the resulting value of n?
 
J

jacob navia

Le 28/06/11 21:43, Ian Collins a écrit :
From Tim Rentsch's post, something like

void foo( int& x )
{
++x;
}

int n = 42;

foo( n );

foo( n++ ); // What is the resulting value of n?

I do not know. It doesn't compile since it is the same as:
int fn(void)
{
int n=42;
int *pn = &n++;
}

That doesn't compile either.

I just do not see what is the problem but very probably it is my fault.
 
J

Jens Gustedt

Am 28.06.2011 21:59, schrieb jacob navia:
Le 28/06/11 21:43, Ian Collins a écrit :
I just do not see what is the problem but very probably it is my fault.

The problem is that you can't get by any means close to the behavior
of C++. In C++ many of the standard operators return lvalues where
they return rvalues in C.

For the example, instead of postfix ++ use

foo(++n);

This would compile in C++ (for foo taking a reference) and give the
user about what (s)he expects, since (in C++) in contrast to n++
(which returns an rvalue) ++n evaluates to an lvalue that represents
the same object as n.

A simple rule that just implicitly creates lvalues from rvalues when
passed to a function with references would have surprising effects
for the programmer.

Introducing references into C without discussing the types of all
standard operators (in terms of rvalue or lvalue) and without
discussing if functions would be allowed to return lvalues makes not
much sense to me.

Jens
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top