By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,776 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

About const reference bounding to temporary variables.

P: n/a
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.

Aug 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
刘昊 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...
Aug 13 '07 #2

P: n/a
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.

Aug 13 '07 #3

P: n/a
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&.

Aug 13 '07 #4

P: n/a
le**********@gmail.com 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
Aug 13 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.