---Something I wanted to mention---
I am not trying to increase the confusion here, just wanted to mention.
According to Microsoft:
Temporary Objects Cannot be Bound to Non-Const References
In previous releases of Visual C++, non-const references could be bound to temporary objects. Now, temporary objects can only be bound to const references.
|
The above was in reference to Microsoft Visual Studios 2008.
http://msdn.microsoft.com/en-us/library/cfbk5ddc(v=vs.90).aspx
So some older compilers, possibly not just Microsoft, may allow usage of non-const reference(./?)
Therefore, design of copy constructors without const were valid with those compilers.
My only assumption is that the compiler would elide the copy constructor call.
---As for what op stated in the previous post---
"explain '...' why a const copy constructor can be called with both lvalue and rvalue arguments while a non-const copy constructor can be called with only lvalues"
|
Cubbi mentioned it as a logic-design statement (I believe).
First let's try to understand lvalue and rvalue (skip if you already know and have a better definition than mines)
lvalue == non-temporary data, that's all it means. (Has more meaning than that but just think of it that way)
Example: string s;
rvalue == temporary data, same comment as above.
Example: "this is my string"
http://en.cppreference.com/w/cpp/language/value_category
So, using functions called with string data (not objects, for an easier understanding):
With const, you could pass (by reference) a string variable or a "this is a string" temporary variable (aka lvalue and rvalue)
Without const, you could pass (by reference) a string variable, but not a "this is a string" temporary variable because omitting const allows you to change the value and address - which isn't really applicable to "this is a string". (The compiler would stop you - hence why it is only possible of lvalue).
The same applies to objects.
const reference:
cents(myCents)
cents(Cents(/*some_data*/)) //casting data to object Cents
without const, with reference:
cents(myCents)
//cents(Cents(/*some_data*/)) - won't compile, casting data to object Cents
That's what I believe Cubbi was stating.