I always believed that temporary objects in C++ are automatically considered as const by the compiler. But recently I experienced that the following example of code:
Temporary objects can be const, but they don't have to be.
((string const)"hello").append(" world"); // error!
It allows for various things. Consider
struct bitref {
int index;
bitref &operator=(bool value); // non-const!
};
struct bitset {
int flags;
// returns a bitref temporary that's associated with the bit
// at index 'index'.
bitref operator[](int index);
// ...
};
You could do
bitset b;
b[1] = true; // change second bit to 1
This is what's done by the std::bitset<> template.