Boost logo

Boost :

From: Csaba Szepesvari (szepes_at_[hidden])
Date: 1999-08-19 02:04:27


> > What do you mean exactly ?
> >
> > The following ?
> >
> > class foo
> > {public:
> > foo (bool initialise) { if (initialise) value = 0; }
> > };
>
> Well, something more like:
>
> class foo {
> public:
> foo(bool initialise = false) { if(initialize) Initialize();
> }
> };
>
> save with template syntax, which I assume we're leaving out for clarity's
> sake. With the Boolean as the last argument, it might also make sense to
> allow yet another argument, to specify the initialization value, as follows:
>
> class foo {
> public:
> foo(value_type& T = nValue,bool initialise = false) {
> if(initialize) Initialize(T); }
> };
>
> where T is the value_type for the container in question (assuming templates,
> of course), and nValue is its value (not necessarily a numeric as my tidbit
> of Hungarian notation implies). Then you'd supply the last two arguments
> only if the Boolean were false. On second thought, I guess you could also
> test the value_type argument and not even have the Boolean, but that might
> unnecessarily complicate the container's value_type, which might get
> expensive with large containers. Besides, as you pointed out earlier, the
> idea is to modify the iterator, not the container being iterated.
>

What about:

class foo {

public:
  template <bool initialise = false>
  foo( value_type& value ) { if (initialise) Initialise(value); }

  template <bool initialise = false>
  foo() { if (initialise) Initialise(default_value); }
};

In this case no moderately smart compiler will generate a line of code for
initialization when initialise == false.
[Unfortunately, if my memories are correct this code won't compile on MS VC++
5.0, Service Pack 3:-( ]
The overhead in coding is not too big and the code does not rely that much on
the optimization capabilities of compilers.

??

- Csaba


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