Boost logo

Boost :

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


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