default value for a variable using template argument

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

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@sympatico.ca> 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; };

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
Andrey Tcherepanov wrote: 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@sympatico.ca> 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

On Fri, Nov 14, 2008 at 13:04, Chris <indy271828@sympatico.ca> 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.
It's an interesting idea, but it requires discipline in changing all the declarations and definitions, so I don't think there's any real gain since I could just use the discipline to add an initialization to the definition instead. Overall, I'll stick with -Wuninitialized and such. ~ Scott

Scott McMurray wrote:
Overall, I'll stick with -Wuninitialized and such.
Non initializing memory is not necessarily bad. Think of boost::optional, for example. It doesn't necessarily initialize the memory, while it does initialize the value it represents. So there are some types when it is important to force initialization and there are some others where it is important not to force it. This is typically the difference between non-PODs and PODs.
participants (4)
-
Andrey Tcherepanov
-
Chris
-
Mathias Gaunard
-
Scott McMurray