Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to protect an array definition againt incomplete initialization with non-zero values?
    primarykey
    data
    text
    <p>I have a global array, which is indexed by the values of an enum, which has an element representing number of values. The array must be initialized by a special value, which unfortunately is not a 0.</p> <pre><code>enum { A, B, C, COUNT }; extern const int arr[COUNT]; </code></pre> <p>In a .cpp file:</p> <pre><code>const int arr[COUNT] = { -1, -1, -1 }; </code></pre> <p>The enum is occasionally changed: new values added, some get removed. The error in my code, which I just fixed was an insufficient number of initialization values, which caused the rest of the array to be initialized with zeroes. I would like to put a safeguard against this kind of error.</p> <p>The problem is to either guarantee that the <code>arr</code> is always completely initialized with the special value (the <code>-1</code> in the example) or to break compilation to get the developers attention, so the array can be updated manually.</p> <p>The recent C++ standards are not available (old ms compilers and some proprietary junk). Templates can be used, to an extent. STL and Boost are strongly prohibited (don't ask), but I wont mind to copy or to reimplement the needed parts.</p> <p>If it turns out to be impossible, I will have to consider changing the special value to be 0, but I would like to avoid that: the special value (the <code>-1</code>) might be a bit too special and encoded implicitly in the rest of the code.</p> <p>I would like to avoid DSL and code generation: the primary build system is jam on ms windows and it is major PITA to get anything generated there.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload