title: What is the best alternative to C's nested array designators in C++ for initializing a 2D array?
body: <p>I am currently trying to convert my small regular expression engine from C to C++.
  To discard syntactically incorrect regexes in a compact way, I use a 2D array to define what kinds of tokens are allowed after one another:</p>
  <pre class="lang-c prettyprint-override"><code>#define NUMBER_OF_TOKEN_KINDS 15
  typedef enum {
      Literal,
      Alternator,
      ...
  } TokenKind;
  
  bool grammar_allowed[NUMBER_OF_TOKEN_KINDS][NUMBER_OF_TOKEN_KINDS] = {
      ...
      [Literal][Alternator] = true,
      [Alternator][Literal] = true,
      ...
  }
  </code></pre>
  <p>With this kind of structure you can easily catch a big percentage of syntax errors by checking if <code>grammar_allowed[previous_token][current_token]</code> is set.</p>
  <p>This way of initializing <code>grammar_allowed</code> doesn't work in C++, because nested array designators are not part of the language. I can think of two working alternatives to the code above, but both have drawbacks:</p>
  <ol>
  <li>Just don't use designators to initialize entries in the array. This makes it significantly less readable, because I can't see at a glance which entry corresponds to which tokens. It would also be annoying to add/remove tokens or fix a wrong initialization.</li>
  </ol>
  <pre class="lang-cpp prettyprint-override"><code>private:
      static constexpr bool m_grammar_allowed[NUMBER_OF_TOKEN_KINDS][NUMBER_OF_TOKEN_KINDS] = {
          {false, true, true, false, false, true, ...},
          ...
      }
  </code></pre>
  <ol start="2">
  <li>Initialize the entries line by line in a constructor. This doesn't lose readability but is slower, because the data has to be loaded in at runtime instead of being stored in the executable itself.</li>
  </ol>
  <pre class="lang-cpp prettyprint-override"><code>Lexer() {
      ...
      m_grammar_allowed[Literal][Alternator] = true;
      m_grammar_allowed[Alternator][Literal] = true;
      ...
  }
  </code></pre>
  <p>Is there any other way to initialize a 2D array like this in C++, but without sacrificing readability or performance?</p>
  <p>Thank you!</p>