473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

About const reference bounding to temporary variables.

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
4 1770
刘昊 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bolin | last post by:
Hi all, A question about smart pointers of constant objects. The problem is to convert from Ptr<T> to Ptr<const T>. I have look up and seen some answers to this question, but I guess I am too...
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
10
by: ATASLO | last post by:
In the following example, section #3 fails under VC98, VC2003, VC2005 Express Beta (Aug 2004) and g++ 3.3.2. Is this just a pitfall of the C++ specification? Why don't any of the above compilers...
5
by: Crirus | last post by:
What do you think about this approaches, wich one is the best? For x As Integer = u.GetViewBounds.X To u.GetViewBounds.X + u.GetViewBounds.Width For y As Integer = u.GetViewBounds.Y To...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
13
by: dragoncoder | last post by:
Hi everyone, please consider the following function:- const int& foo ( const double& d ) { return d; } g++ compiles it with warnings and solaris CC gives error. I want to know if the code...
10
by: flopbucket | last post by:
Hi, Is this legal? std::string foo() { std::string xyz = "FOO"; return xyz; }
4
by: cxf | last post by:
class A{ public: A() { func(0); }; virtual void func(int data) { printf("A1 :%d\n",data); }
2
by: casul | last post by:
Hi All, Given the following code : char const & identity( char const &c ) { return c; } char const & f( ) { return identity('A'); } char const & g( ) { return 'A'; } Of course, the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.