Add bool initializeToZero to HeapBlock constructor

If we want a lifetime pass to ride the RAII train then this HeapBlock constructor should have an additional boolean, default to false, to initialize the specified elements to zero:


HeapBlock (const size_t numElements, bool initializeToZero = false)
  if (initializeToZero)
    clear (numElements);

Thanks for the suggestion, I’ve added something similar.

For such a commonly-used method, adding a second parameter which is almost never needed would have bloated everyone’s code unnecessarily, but it works fine as a separate constructor. It’s also the kind of place where an enum works better than a bool, so that calling code will be more readable.

LOL…Jules! Did we forget that HeapBlock is a template? Accessing the enumeration requires a template argument list. This is my ctor-initializer now:

struct MyClass
  MyClass ();
  HeapBlock <Page*> m_array;

MyClass::MyClass () : m_array (64, HeapBlock <Page*>::clearToZero)

Usually I work around this by adding a “helper” base class to factor out declarations that are independent of the template argument list:

class HeapBlockBase
    /** Flags used to indicate whether a newly allocated block should be cleared or not. */
    enum InitialisationState
        leaveUnitialised = 0,

And then just derive HeapBlock from HeapBlockBase.

Goddammit. Yes, I did forget that! Fair enough… I don’t really want to leave the enum floating in the juce namespace, so maybe a bool is indeed the best bet!

Well if it makes you feel any better…I didn’t realize it either until I got a compile error using HeapBlock::clearToZero.

I agree about having a loose enum, but what’s your objection to using the base class? It’s a fairly common idiom, normally used to reduce template instantiations, by putting functions that are independent of the template parameter into a base class. In boost these go into the “detail” namespace.

Yeah… I just don’t like the extra class idea aesthetically, hard to describe exactly why. I just think that a class like that would clutter up the class list while adding almost nothing of value. People would see “HeapBlock” and “HeapBlockBase” in the list, and have to waste time reading about each one to figure out what the difference is and which one to use.