|
Boost : |
From: Beman Dawes (bdawes_at_[hidden])
Date: 2001-06-13 21:10:06
At 08:51 PM 6/13/2001, David Abrahams wrote:
>
>----- Original Message -----
>From: "Luis Pedro Coelho" <deepblack9_at_[hidden]>
>
>> template <typename T, T init = T()>
>> 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;
>> };
>>
>> Copyright is by me (I do not recall seeing this anywhere) but I place
no
>> restrictions on the use of the above code.
>>
>> I have added a little function called reset() to reset the object to
its
>> initial value because I have found use for it. I originally kept the
>object
>> public, but have never found a need for it, so I changed it to private.
>>
>> I think this small class could go into boost/utility.hpp.
>>
>> What do you think?
>
>
>I think it's nice, but would prefer to call the template "initialized".
>Also, I note that it /only/ works with built-in integer types. What about
>pointers and floating types? I think these deserve an "initialized" type
as
>well.
Also, documentation and a test program would help. And are there any known
limitations on the object's usage?
--Beman
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk