Using a C++ user-defined literal to initialise an array : <p>I have a bunch of test vectors, presented in the form of hexadecimal strings:</p>

<pre><code>MSG: 6BC1BEE22E409F96E93D7E117393172A
MAC: 070A16B46B4D4144F79BDD9DD04A287C
MSG: 6BC1BEE22E409F96E93D7E117393172AAE2D8A57
MAC: 7D85449EA6EA19C823A7BF78837DFADE
</code></pre>

<p>etc. I need to get these into a C++ program somehow, without too much editing required. There are various options:</p>

<ul>
<li>Edit the test vectors by hand into the form <code>0x6B,0xC1,0xBE,...</code></li>
<li>Edit the test vectors by hand into the form "6BC1BEE22E409F96E93D7E117393172A" and write a function to convert that into a byte array at run time.</li>
<li>Write a program to parse the test vectors and output C++ code.</li>
</ul>

<p>But the one I ended up using was:</p>

<ul>
<li>User-defined literals,</li>
</ul>

<p>because fun. I defined a helper class <code>HexByteArray</code> and a user-defined literal operator <code>HexByteArray operator "" _$ (const char* s)</code> that parses a string of the form <code>"0xXX...XX"</code>, where <code>XX...XX</code> is an even number of hex digits. <code>HexByteArray</code> includes conversion operators to <code>const uint8_t*</code> and <code>std::vector&lt;uint8_t&gt;</code>. So now I can write e.g.</p>

<pre><code>struct {
  std::vector&lt;uint8_t&gt; MSG ;
  uint8_t* MAC ;
  } Test1 = {
  0x6BC1BEE22E409F96E93D7E117393172A_$,
  0x070A16B46B4D4144F79BDD9DD04A287C_$
  } ;
</code></pre>

<p>Which works nicely. But now here is my question: Can I do this for arrays as well? For instance:</p>

<pre><code>uint8_t MAC[16] = 0x070A16B46B4D4144F79BDD9DD04A287C_$ ;
</code></pre>

<p>or even</p>

<pre><code>uint8_t MAC[] = 0x070A16B46B4D4144F79BDD9DD04A287C_$ ;
</code></pre>

<p>I can't see how to make this work. To initialise an array, I would seem to need an <code>std::initializer_list</code>. But as far as I can tell, only the compiler can instantiate such a thing. Any ideas?</p>

<hr>

<p>Here is my code:</p>

<p><code>HexByteArray.h</code></p>

<pre><code>#include &lt;cstdint&gt;
#include &lt;vector&gt;

class HexByteArray
  {
public:
  HexByteArray (const char* s) ;
  ~HexByteArray() { delete[] a ; }

  operator const uint8_t*() &amp;&amp; { const uint8_t* t = a ; a = 0 ; return t ; }
  operator std::vector&lt;uint8_t&gt;() &amp;&amp;
    {
    std::vector&lt;uint8_t&gt; v ( a, a + len ) ;
    a = 0 ;
    return v ;
    }

  class ErrorInvalidPrefix { } ;
  class ErrorHexDigit { } ;
  class ErrorOddLength { } ;

private:
  const uint8_t* a = 0 ;
  size_t len ;
  } ;

inline HexByteArray operator "" _$ (const char* s)
  {
  return HexByteArray (s) ;
  }
</code></pre>

<p><code>HexByteArray.cpp</code></p>

<pre><code>#include "HexByteArray.h"

#include &lt;cctype&gt;
#include &lt;cstring&gt;

HexByteArray::HexByteArray (const char* s)
  {
  if (s[0] != '0' || toupper (s[1]) != 'X') throw ErrorInvalidPrefix() ;
  s += 2 ;

  // Special case: 0x0_$ is an empty array (because 0x_$ is invalid C++ syntax)
  if (!strcmp (s, "0"))
    {
    a = nullptr ; len = 0 ;
    }
  else
    {
    for (len = 0 ; s[len] ; len++) if (!isxdigit (s[len])) throw ErrorHexDigit() ;
    if (len &amp; 1) throw ErrorOddLength() ;
    len /= 2 ;
    uint8_t* t = new uint8_t[len] ;
    for (size_t i = 0 ; i &lt; len ; i++, s += 2)
      sscanf (s, "%2hhx", &amp;t[i]) ;
    a = t ;
    }
  }
</code></pre>
