C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to initialise a reference in g++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Arik Funke
Guest





PostPosted: Tue Oct 26, 2004 7:47 pm    Post subject: how to initialise a reference in g++ Reply with quote



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.
}
Back to top
JKop
Guest





PostPosted: Tue Oct 26, 2004 7:59 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote



Arik Funke posted:

Quote:
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


Back to top
Nils O. Selåsdal
Guest





PostPosted: Tue Oct 26, 2004 8:01 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote



On Tue, 26 Oct 2004 12:47:58 -0700, Arik Funke wrote:

Quote:
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.


Back to top
Victor Bazarov
Guest





PostPosted: Tue Oct 26, 2004 8:04 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote

Arik Funke wrote:
Quote:
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.

Quote:
Is there a universal coding standard?

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

Quote:

Cheers,
Arik

---
class abc {
etc.etc.

You mean
// etc.etc.

Quote:
}
;

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

Can't do that. Have to do

abc const & ref = abc();

Quote:
etc.etc.

You mean
// etc.etc.

Quote:
}

V

Back to top
Howard
Guest





PostPosted: Tue Oct 26, 2004 8:04 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote


"Arik Funke" <arik.funke (AT) gmx (DOT) de> wrote

Quote:
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




Back to top
Rob Williscroft
Guest





PostPosted: Tue Oct 26, 2004 8:19 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote

JKop wrote in news:i0yfd.40101$Z14.14684 (AT) news (DOT) indigo.ie in
comp.lang.c++:

Quote:

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.

Quote:

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.

Quote:
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.
--
http://www.victim-prime.dsl.pipex.com/

Back to top
JKop
Guest





PostPosted: Tue Oct 26, 2004 8:26 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote

Rob Williscroft posted:

Quote:
JKop wrote in news:i0yfd.40101$Z14.14684 (AT) news (DOT) indigo.ie 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.


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


Quote:
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.


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.


Quote:
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.


We all make typos...


Quote:
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

Back to top
Brad Herald
Guest





PostPosted: Tue Oct 26, 2004 8:57 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote

gcc is not the same g++... make sure you use the right compiler


"Arik Funke" <arik.funke (AT) gmx (DOT) de> wrote

Quote:
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.
}



Back to top
Adrian
Guest





PostPosted: Tue Oct 26, 2004 9:54 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote

JKop wrote:
Quote:
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

Back to top
Rob Williscroft
Guest





PostPosted: Tue Oct 26, 2004 10:54 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote

JKop wrote in news:Ipyfd.40105$Z14.14420 (AT) news (DOT) indigo.ie in
comp.lang.c++:

Quote:
Rob Williscroft posted:

JKop wrote in news:i0yfd.40101$Z14.14684 (AT) news (DOT) indigo.ie in
comp.lang.c++:



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.

Quote:


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.


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 ?

Quote:

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.

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


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.


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.

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

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Back to top
JKop
Guest





PostPosted: Wed Oct 27, 2004 8:02 am    Post subject: Re: how to initialise a reference in g++ Reply with quote


Quote:
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.


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


Quote:
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.


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 ?


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


Quote:

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.


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

Back to top
DaKoadMunky
Guest





PostPosted: Wed Oct 27, 2004 8:18 am    Post subject: Re: how to initialise a reference in g++ Reply with quote

Quote:
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."


Back to top
Arik Funke
Guest





PostPosted: Wed Oct 27, 2004 4:10 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote

"Howard" <alicebt (AT) hotmail (DOT) com> wrote

Quote:
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

Back to top
Victor Bazarov
Guest





PostPosted: Wed Oct 27, 2004 4:23 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote

Arik Funke wrote:
Quote:
"Howard" <alicebt (AT) hotmail (DOT) com> wrote


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?

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

Back to top
Howard
Guest





PostPosted: Wed Oct 27, 2004 4:39 pm    Post subject: Re: how to initialise a reference in g++ Reply with quote


"Arik Funke" <arik.funke (AT) gmx (DOT) de> wrote

Quote:
"Howard" <alicebt (AT) hotmail (DOT) com> wrote

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

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





Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.