|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2002-08-21 16:49:59
From: "Fernando Cacciola" <fcacciola_at_[hidden]>
>
> I'm about to pack up this utility class that we talked about recently,
but I
> need to settle on the semantics.
>
> AFAICT, there are two choices:
>
> A) Trivial approach without deep-constantness.
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> template<class T>
> class value_initialized
> {
> public :
>
> value_initialized() : x() {}
>
> operator T&() { return x ; }
> T& data() { return x ; }
>
> private :
>
> T x ;
> } ;
>
<snip>
>
> B) Deep-constant but hacky approach:
> ~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> template<class T>
> class value_initialized
> {
> public :
>
> value_initialized() : x() {}
>
> operator T&() const { return get() ; }
>
> T& data() const { return get() ; }
>
> private :
>
> T& get () const { return const_cast< value_initialized<T>* >(this)->x
> ; }
>
> T x ;
> } ;
<snip>
>
> Which one do you prefer?
>
> I prefer (B) since, for instance, if I have a value_initialized<> object
> handled through a 'const&', such as when they are fed to generic code
with
> the signature: foo( T const& );
Yeah, but in case (B):
value_initialized<int> const cx;
int& x = cx;
x = 1;
why don't you do something like:
operator T const() const;
in the case where T is non-const? You just need to dispatch to one of 2
implementations for this, depending on T's const-ness.
-----------------------------------------------------------
David Abrahams * Boost Consulting
dave_at_[hidden] * 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