Boost logo

Boost :

From: Luis Pedro Coelho (deepblack9_at_[hidden])
Date: 2001-06-14 09:26:03


Em Quarta, 13 de Junho de 2001 23:43, escreveste:
<snip>
> > template <typename T, T init = T()>
>
> Given the intended usage for this class, I think that the 'init' value
> should be always given.
> This removes the 'default constructible' requirement on T imposed by the
> default initialization of 'init'.

My usage of this has always been with builtin types so that has never been a
problem. In general, I think value classes should have default constructors,
so I do not see this as a problem.

> > struct auto_init {
> > auto_init():object(init) { }
> > auto_init(const T& other):object(other) { }
> > operator T& () { return object; }
> > operator const T& () const { return object; }
> > T& operator = (const T& other) { object = other; return object; }
> > void reset() { object = init; }
> > private:
> > T object;
> > };
>
> Since you are providing conversion operators, the constructor for T should
> be explicit.
> Therefore, auto_init's own copy constructor and assigment should also be
> present.
>
> But there are other important issues to be addressed:
> Ideally, auto_init<T> objects should inherit the semantics of the inner
> type T,
> such as other operators (==,+,&, void const*, etc...) but that's tricky,
> because T might not support some of the them.
> Otherwise I won't be able to write expressions like: if ( data == 3 ),
> int a = data + 1 , int* p = &data, if ( data ) ..., etc...
<snip>

There except for the taking of the address, all other cases are handled by
the conversion to T operator. For example, "if (data == 3)" gets transformed
into "if (data.operator int() == 3)", so no problems there.

Luis


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