Kira Yamato wrote:
If so, is there a good reason why the C++ designer chose it this way?
It helps avoiding some issues arising from integral promotion. Otherwise,
it is just a nuisance. For instance, you can do:
MyClass & operator= ( MyClass const & other ) {
MyClass( other ).swap( *this );
return ( *thid );
}
but not
MyClass & operator= ( MyClass const & other ) {
this->swap( MyClass( other ) );
return ( *this );
}
You also cannot use a temporary to initialize a default non-const
parameter:
void search ( SearchClass & initial_pos = SearchClass() );
but if you provide a member function
class SearchClass {
...
SearchClass & me ( void ) {
return ( *this );
}
};
you can do:
void search ( SearchClass & initial_pos = SearchClass().me() );
And for standard classes that do not support a me() method, you could do:
void grow ( std::vector<int> & the_list =
( std::vector<int>() = std::vector<int>() ) );
Since the assignment operattor returns a non-const reference, the result
will happily bind to the parameter.
As you can see, it is not very logical at all.
The next version of the standard will include r-value references which
hopefully will put an end to this nonsense.
As far as I know, a temporary object lives on the stack, and there
should be no reason why it should not be modified.
There isn't and you can modify temporaries at will. You just cannot bind
them to non-const references directly.
Best
Kai-Uwe Bux
Thank you for your very detailed explanation.
It's beginning to dawn on me that many rules in C++ can be bent.