how to initialise a reference in g++

A

Arik Funke

Hi together,

following code does compile fine on MS VC but not on gcc. What am I
doing wrong? Is there a universal coding standard?

Cheers,
Arik

---
class abc {
etc.etc.
}

void def() {
abc &ref = abc();
etc.etc.
}
 
J

JKop

Arik Funke posted:
Hi together,

following code does compile fine on MS VC but not on gcc. What am I
doing wrong? Is there a universal coding standard?

Cheers,
Arik

---
class abc {
etc.etc.
}

void def() {
abc &ref = abc();
etc.etc.
}


As usual, Visual C++ is wrong.


abc const &ref = abc();


would be valid. Here's the (weird) reason:


Firstly, a few points:

1) Temporaries are *not* const.

2) A temporary is an r-value, *not* an l-value.

For a class, this makes only *one* difference, which is:

You cannot bind a reference to a temporary... unless the reference is const.


-JKop
 
?

=?iso-8859-1?q?Nils_O=2E_Sel=E5sdal?=

Hi together,

following code does compile fine on MS VC but not on gcc. What am I
doing wrong? Is there a universal coding standard?
Cheers,
Arik

---
class abc {
etc.etc.
}

void def() {
abc &ref = abc();
etc.etc.
}
What does the compiler say is wrong ? -Wall flag can be useful as well.
 
V

Victor Bazarov

Arik said:
following code does compile fine on MS VC but not on gcc. What am I
doing wrong?

You're trying to bind a reference to non-const 'abc' to a temporary.
Is there a universal coding standard?

Yes, it's called "INCITS/ISO/IEC 14882-2003 Programming Languages - C++".
It's an international standard.
Cheers,
Arik

You mean
// etc.etc.
} ;

void def() {
abc &ref = abc();

Can't do that. Have to do

abc const & ref = abc();

You mean
// etc.etc.

V
 
H

Howard

Arik Funke said:
Hi together,

following code does compile fine on MS VC but not on gcc. What am I
doing wrong? Is there a universal coding standard?

Cheers,
Arik

---
class abc {
etc.etc.
}

void def() {
abc &ref = abc();
etc.etc.
}

Any particular reason you want to create a new object, but assign it to a
reference? Why are you declaring it as a reference? References are
intended to "refer" to an existing object, and here the object it refers to
is merely a "temporary", having no reason to exist other than to assign it
to the variable ref. Can't you just declare ref to be an object of type
abc, not a reference to one? (The code you've shown certainly doesn't
suggest any reason to be using a reference.)

-Howard
 
R

Rob Williscroft

JKop wrote in in
comp.lang.c++:
As usual, Visual C++ is wrong.


abc const &ref = abc();


would be valid. Here's the (weird) reason:


Firstly, a few points:

1) Temporaries are *not* const.

Not true, some are some aren't.
2) A temporary is an r-value, *not* an l-value.

Not true, some are some aren't, temporary-ness and l/r-value-ness
are orthogonal concepts.
For a class, this makes only *one* difference, which is:

You cannot bind a reference to a temporary... unless the reference is
const.

No, its because the standard say's so.

Also your logic, even if it were based on fact's, is completly
screwed. I.e. your conclusion is unrelated to *any* of your
premises.

Rob.
 
J

JKop

Rob Williscroft posted:
JKop wrote in in
comp.lang.c++:


Not true, some are some aren't.


I've never seen a const temporary. Would you care to provide an example?

Not true, some are some aren't, temporary-ness and l/r-value-ness
are orthogonal concepts.


Temporaries:

1) "TypeName()" is a temporary.

2) The return value of a function is a temporary.


l-value-ness does indeed make a difference for the intrinsic types. The
following is illegal:

int Func() { return 5; }

int main()
{
Func() = 5;
}

and it's not because it's const, it's because it's an r-value.


But the following *is* legal:

AnyClass Func() { return AnyClass(); }

int main
{
Func() += 5;
}


The only place where "l-value/r-value -ness" affects a class type is when
trying to bind to a reference.

BTW, "l-value" and "r-value" are expicitly defined in the Standard.

No, its because the standard say's so.


We all make typos...

Also your logic, even if it were based on fact's, is completly
screwed. I.e. your conclusion is unrelated to *any* of your
premises.


But two misplaced apostraphes within throwing distance of each other.


-JKop
 
A

Adrian

JKop said:
2) A temporary is an r-value, *not* an l-value.

Side issue, but doesn't the c++ standard only mention l-value's not
r-values or am I thinking of the c standard.

Adrian
 
R

Rob Williscroft

JKop wrote in in
comp.lang.c++:
Rob Williscroft posted:
I've never seen a const temporary. Would you care to provide an
example?

struct X {};

X const &cref = X();

The rvalue X() is converted to a constant temporary prior to being
bound to the reference, the temporary is an lvalue.
Temporaries:

1) "TypeName()" is a temporary.

2) The return value of a function is a temporary.


l-value-ness does indeed make a difference for the intrinsic types.
The following is illegal:

int Func() { return 5; }

int main()
{
Func() = 5;
}

and it's not because it's const, it's because it's an r-value.

Yes, but so what ?
But the following *is* legal:

AnyClass Func() { return AnyClass(); }

int main
{
Func() += 5;
}


The only place where "l-value/r-value -ness" affects a class type is
when trying to bind to a reference.

Type and l/r-value-ness are orthogonal.
BTW, "l-value" and "r-value" are expicitly defined in the Standard.




We all make typos...




But two misplaced apostraphes within throwing distance of each other.

Did you mean apostrophes?, (there is a usnet law about this IIRC).

Rob.
 
J

JKop

struct X {};

X const &cref = X();

The rvalue X() is converted to a constant temporary prior to being
bound to the reference, the temporary is an lvalue.


That's pure speculation. Unless you've got something from the Standard, it's
bullshit.


int x = 5;


int const &k = x;


I suppose "x" is "converted" to constant here... ?

Yes, but so what ?


It's relevant, that's what. The OP wants to know why you can't bind a r-
value to a non-const reference.

Type and l/r-value-ness are orthogonal.


Did some-one get a new dictionary for Christmas?


Courtesy of "dictionary.com":

or·thog·o·nal ( P ) Pronunciation Key (ôr-thg-nl)
adj.
Relating to or composed of right angles.
Mathematics.
Of or relating to a matrix whose transpose equals its inverse.
Of or relating to a linear transformation that preserves the length of
vectors.


How the hell do you apply this term as you do?


-JKop
 
D

DaKoadMunky

Did some-one get a new dictionary for Christmas?
Courtesy of "dictionary.com":

or·thog·o·nal ( P ) Pronunciation Key (ôr-thg-nl)
adj.
Relating to or composed of right angles.
Mathematics.
Of or relating to a matrix whose transpose equals its inverse.
Of or relating to a linear transformation that preserves the length of
vectors.


How the hell do you apply this term as you do?

Did you read the whole page? Apparently not...

http://dictionary.reference.com/search?q=orthogonal

....a portion of which reads "Mutually independent; well
separated; sometimes, irrelevant to."
 
A

Arik Funke

Howard said:
reference? Why are you declaring it as a reference? References are
intended to "refer" to an existing object, and here the object it refers to
is merely a "temporary", having no reason to exist other than to assign it
to the variable ref. Can't you just declare ref to be an object of type
abc, not a reference to one? (The code you've shown certainly doesn't
suggest any reason to be using a reference.)

-Howard

There is a reason: I later in the code I would like the reference to
hold the return value of a function of type abc. Thus I first need to
create a reference that then can be assigned the object... Or not? And
since you cannot have an empty reference I need to have a temporary
abc type assigned to it? - Or am I missing something?

- Arik
 
V

Victor Bazarov

Arik said:
There is a reason: I later in the code I would like the reference to
hold the return value of a function of type abc. Thus I first need to
create a reference that then can be assigned the object... Or not? And
since you cannot have an empty reference I need to have a temporary
abc type assigned to it? - Or am I missing something?

You're probably also missing the fact that references cannot be re-seated.
Once a reference is made to refer to an object, it will refer to it until
its lifetime is over.

The code you posted is too abstract and contains too little information to
suggest anything of value. Try to be more specific and post your code
together with your thoughts.

V
 
H

Howard

Arik Funke said:
There is a reason: I later in the code I would like the reference to
hold the return value of a function of type abc. Thus I first need to
create a reference that then can be assigned the object... Or not? And
since you cannot have an empty reference I need to have a temporary
abc type assigned to it? - Or am I missing something?

- Arik

You can't do that. Once a reference is assigned, it's there to stay. You
cannot later assign anything else to it. I believe your compiler would
refuse such an attempt. If you need to assign different objects to the same
variable over time, then you can use a pointer, but not a reference.

What does the function return? An object? A pointer? A reference to an
existing object? (And can you change it to return a pointer if it now
returns a reference?) More code would be needed for us to make any further
recommendations.

-Howard
 

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

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top