Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-09-04 13:45:19


Howard Hinnant <hinnant_at_[hidden]> writes:

> On Sep 1, 2004, at 6:06 PM, Stefan Slapeta wrote:
>
>>
>> see http://tinyurl.com/5639d
>>
>> 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

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk