[utility] value_init fails on CW*

see http://tinyurl.com/5639d Has anybody a solution for this? Seems that all CWs have problems with casts in combination with conversion functions. Stefan

Stefan Slapeta <stefan_nospam_@slapeta.com> writes:
Has anybody a solution for this? Seems that all CWs have problems with casts in combination with conversion functions.
template<class T> void test ( T const& y, T const& z ) { boost::value_initialized<T> x ; BOOST_TEST ( y == x ) ; BOOST_TEST ( y == get(x) ) ; static_cast<T&>(x) = z ; get(x) = z ; BOOST_TEST ( x == z ) ; boost::value_initialized<T> const x_c ; BOOST_TEST ( y == x_c ) ; BOOST_TEST ( y == get(x_c) ) ; static_cast<T&>(x_c) = z ; ### mwcc Compiler: # File: ..\libs\utility\value_init_test.cpp # -------------------------------------------- # 76: static_cast<T&>(x_c) = z ; # Error: ^ # constness casted away The only solution I can think of is to change the test: T& t_ref = x_c; t_ref = z; or implicit_cast<T&>(x_c) = z; -- Dave Abrahams Boost Consulting http://www.boost-consulting.com

"David Abrahams" <dave@boost-consulting.com> escribió en el mensaje news:u3c215tym.fsf@boost-consulting.com...
Stefan Slapeta <stefan_nospam_@slapeta.com> writes:
Has anybody a solution for this? Seems that all CWs have problems with casts in combination with conversion functions.
[SNIPED]
### mwcc Compiler: # File: ..\libs\utility\value_init_test.cpp # -------------------------------------------- # 76: static_cast<T&>(x_c) = z ; # Error: ^ # constness casted away
The only solution I can think of is to change the test:
T& t_ref = x_c; t_ref = z;
This one commited. Thank you! Fernando Cacciola SciSoft

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? -Howard

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
participants (4)
-
David Abrahams
-
Fernando Cacciola
-
Howard Hinnant
-
Stefan Slapeta