Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > About const reference bounding to temporary variables.

Reply
Thread Tools

About const reference bounding to temporary variables.

 
 
=?utf-8?B?5YiY5piK?=
Guest
Posts: n/a
 
      08-13-2007
Hi, folks,
Look at the following snippet:

int main()
{
double temp = 2.0;
const double& r = 2.0 + temp; // without const, an err will be
raised

return 0;
}

As the comment block illustrated, without "const" qualifier

error: could not convert `(temp + 2.0e+0)' to `double&'

will be raised, I know, you can't use an expression to initialize a
reference, but why it's OK with const qualifier? Thanks, you guys.

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-13-2007
刘昊 wrote:
> Hi, folks,
> Look at the following snippet:
>
> int main()
> {
> double temp = 2.0;
> const double& r = 2.0 + temp; // without const, an err will be
> raised
>
> return 0;
> }
>
> As the comment block illustrated, without "const" qualifier
>
> error: could not convert `(temp + 2.0e+0)' to `double&'
>
> will be raised, I know, you can't use an expression to initialize a
> reference, but why it's OK with const qualifier? Thanks, you guys.


The answer is ... because the C++ specification says so.

I think it is because it conforms to the "path of least surprise" rule.
It makes little sense to modify the result of an expression (a
temporary). There are other probably better reasons...


 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      08-13-2007
Thank you, GM,

I think I've figured out what's going on here.

....
double& r = 2.0+temp;
....

The compiler generates a temporary variable we can not see to
initialize the reference, it makes no sense to let an expression be a
L value, so the compiler enforces this by raising an error if we
remove the "const" qualifier before the reference.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-13-2007
Gianni Mariani wrote:

> ?? wrote:
>> Hi, folks,
>> Look at the following snippet:
>>
>> int main()
>> {
>> double temp = 2.0;
>> const double& r = 2.0 + temp; // without const, an err will be
>> raised
>>
>> return 0;
>> }
>>
>> As the comment block illustrated, without "const" qualifier
>>
>> error: could not convert `(temp + 2.0e+0)' to `double&'
>>
>> will be raised, I know, you can't use an expression to initialize a
>> reference, but why it's OK with const qualifier? Thanks, you guys.

>
> The answer is ... because the C++ specification says so.
>
> I think it is because it conforms to the "path of least surprise" rule.
> It makes little sense to modify the result of an expression (a
> temporary).


If that was the reason, the standard could just say that temporaries are
const. However, it is perfectly legal to modify temporaries: you can call
non-const member functions on temporaries.

In fact, you can use this to circumvent the language restriction on binding
a temporary to a non-const reference:

struct I_can_bind {


I_can_bind & me ( void ) {
return ( *this );
}

};

void some_fct ( I_can_bind & ref ) {}

some_fct( I_can_bind().me() );


> There are other probably better reasons...


One thing is automatic conversions. Consider:


void inc ( double & d ) {
d += 1.0;
}
...
int i = 5;
...
inc( i );


In this case, a temporary double would be constructed from i and then the
temporary would be incremented instead of i.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Guest
Posts: n/a
 
      08-13-2007
First, thank you, KUB,

> One thing is automatic conversions. Consider:
>
> void inc ( double & d ) {
> d += 1.0;
> }
> ...
> int i = 5;
> ...
> inc( i );
>
> In this case, a temporary double would be constructed from i and then the
> temporary would be incremented instead of i.

Have you tried on your compiler(s)? I've tried both on g++ and cl,
none of them will get through without error, they both complained that
you can not convert an int to double&.



 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      08-13-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> First, thank you, KUB,
>
>> One thing is automatic conversions. Consider:
>>
>> void inc ( double & d ) {
>> d += 1.0;
>> }
>> ...
>> int i = 5;
>> ...
>> inc( i );
>>
>> In this case, a temporary double would be constructed from i and then the
>> temporary would be incremented instead of i.

> Have you tried on your compiler(s)? I've tried both on g++ and cl,
> none of them will get through without error, they both complained that
> you can not convert an int to double&.


May I remind you that we are discussing the rational for C++ not allowing to
initialize a non-const reference from a temporary. The code above _would_
compile in C++ if that provision want not in the standard. See the
following (compiles)

void inc ( double const & d ) {
// d += 1.0;
}

int main ( void ) {
int i = 5;
inc( i );
}

versus (does not compile)

void inc ( double & d ) {
d += 1.0;
}

int main ( void ) {
int i = 5;
inc( i );
}

People wanted the later to not compile and that is one of the reasons that
C++ prohibits binding temporaries to non-const variables.


Best

Kai-Uwe Bux
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
non-const reference and const reference George2 C++ 10 12-17-2007 02:19 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
How to fix "Warning: should not initialize a non-const reference with a temporary"? qazmlp C++ 4 05-06-2004 08:54 AM
A reference to non-const to be bound to a temporary object John Ky C++ 9 02-23-2004 12:53 AM
The temporary vs non-const reference love story =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunion?= C++ 13 11-06-2003 02:52 PM



Advertisments