Oleg Goldshmidt wrote:
It appears, however, that client code can fail to compile, and yet temps will not be const.Well, while it was making the round trip to the list and back, I also made a trip to the bookshelf to check myself. Item 15 of Scott Meyers's "Effective C++" confirms what I wrote: temps are const, and for the reasons I covered. It explicitly says that with a declaration like
C& C::operator=(C& rhs) { ... }
client code won't compile.
Then you haven't understood my "dual existence" statement. Try out the following program:
A temp is a non-const, but it cannot be bound to a non-const#include <iostream>
using namespace std;
class a { public: void function() const { cout<<"Const version called"<<endl; } void function() { cout<<"Non-const version called"<<endl; } static void func2( const a & ) { cout<<"Called with const reference"<<endl; } static void func2( a & ) { cout<<"Called with non-const reference"<<endl; } };
int main() { a().function(); a::func2(a());
return 0;
}
reference. I don't like it, but that seems to be the case.
I don't understand this statement. In "a().function()" your temp is not const, according to your wishes. In "a::func2(a())" the compiler-generated temp is const, as appropriate.
Then please explain what makes the first and the second different.
If you don't believeWhile it is indeed nice of you to point out to me the very exact same fact I'm trying to point out to you and say "told you so". In other words, I really lost you.
me, comment out the declaration of "void func2(const a&)" (you are
trying to call "void func2( a & )" if I understand your drift
correctly) and try to compile.
There is no difference between the first a() and the second a(). Both are temporary variables of type a generated by an empty constructor. However, we can see from the sample program that this variable can be used for a non-const method, thus plainly contradicting that temporary variables are const. Then again, we see from the second line that it cannot be bound to a non-const reference. Thus, you get my claim that they have a duel semantics.
This is in complete agreement with what I wrote before (and what isNo. You said temps are const. We can see from the first line of the sample program above that they are clearly not (at least, not always).
still quoted above).
Shachar
-- Shachar Shemesh Lingnu Open Source Consulting ltd. http://www.lingnu.com/
================================================================= To unsubscribe, send mail to [EMAIL PROTECTED] with the word "unsubscribe" in the message body, e.g., run the command echo unsubscribe | mail [EMAIL PROTECTED]
