
Howard Hinnant <hinnant@twcny.rr.com> writes:
On Sep 1, 2004, at 6:06 PM, Stefan Slapeta wrote:
Has anybody a solution for this? Seems that all CWs have problems with casts in combination with conversion functions.
I'm not clear on why this code is expected to work.
template<class T> class value_initialized { public : value_initialized() {} operator T&() const { return this->x ; } T& data() const { return this->x ; } private: mutable T x; } ;
template<class T> void test (T const& z ) { value_initialized<T> const x ; static_cast<T&>(x) = z ; }
int main() { int y = 1; test(y); }
5.2.9/1 says:
The static_cast operator shall not cast away constness (expr.const.cast).
And x is const value_initialized<int>. Even though value_initialized<int> provides a const member function to do the conversion, does not use of static_cast<T>(const_u) prohibit casting from a const type to a non-const type?
I guess it depends what you think "cast away constness" means. I read it as meaning that it shall not remove constness from a given object. But you don't need to remove the constness of x in order to convert it to a T&. -- Dave Abrahams Boost Consulting http://www.boost-consulting.com