Boost logo

Boost :

Subject: Re: [boost] default value for a variable using template argument
From: Chris (indy271828_at_[hidden])
Date: 2008-11-14 19:56:19


Andrey Tcherepanov wrote:
> Hi Chris
>
> some points
>
> a. with upcoming c++0X there was a proposal to have values initialized
> in the class declaration, so that will cover your case, right?
Correct, I posted on the gcc list about N2756, but the implementation
may take a couple of years and not all compilers will have it. I made
this class in the meantime. Also C++0x will handle non-const cases.
>
> b. every time when you introduce new instatiation of template, it will
> make another class. Which (IIUC) will make _width and _heigh not
> comparable unless you introduce some logic to do so.
Didn't think of that, haven't used it like that yet. Only simple cases.
>
> c. (MvHO) I don't find it improving readability at all... :)
It's awkward looking. I find it more useful for variables I forget to
initialize in the constructor, or have many constructors. I don't
think that gcc will warn you on this.
Makes things safe for me until N2756 comes out.
>
> Regards,
> Andrey
>
> On Fri, 14 Nov 2008 11:04:20 -0700, Chris <indy271828_at_[hidden]>
> wrote:
>
>> Hi, did anyone ever suggest a template class which initializes a
>> variable to a certain value? (see example) Is this too trivial?
>> This is useful to prevent uninitialized values in objects.
>>
>> class A
>> {
>> defaulted<int, 1280> _width;
>> defaulted<int, 1024 > _height;
>> defaulted<int, 0> _depth;
>> };
>>
>> template <typename TYPE, TYPE value>
>> class defaulted
>> {
>> public:
>> defaulted() : _t(value) { }
>> defaulted(const TYPE & value2) : _t(value2) { }
>>
>> operator TYPE & () { return _t; }
>> TYPE * operator & () { return &_t; }
>> const TYPE * operator & () const { return &_t; }
>> operator const TYPE & () const { return _t; }
>> TYPE & operator =(const TYPE & other) { _t = other; return _t; }
>> private:
>> TYPE _t;
>> };
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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