Boost logo

Boost :

Subject: Re: [boost] default value for a variable using template argument
From: Andrey Tcherepanov (moyt63c02_at_[hidden])
Date: 2008-11-14 15:29:48


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?

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.

c. (MvHO) I don't find it improving readability at all... :)

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;
> };


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