title: Using array as map key: impossible even with custom allocator?
body: <p>This question is related to <a href="https://stackoverflow.com/questions/2582529/using-array-as-map-value-cant-see-the-error">these</a> <a href="https://stackoverflow.com/questions/19347049/error-array-used-as-initializer-and-i-dont-know-the-error">three</a> <a href="https://stackoverflow.com/questions/8200471/what-is-the-best-way-to-use-the-c-type-uuid-t-as-a-key-in-stl-stdmap">questions</a>.</p>
  
  <p>I was trying to use a fixed-length array as a key to a <code>std::map</code>, as demonstrated in the non-compiling code below:</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;map&gt;
  
  typedef char myuuid[ 16 ];
  
  template &lt;class T&gt;
  class MyAlloc
  {
  public:
    typedef T value_type;
    typedef T* pointer;
    typedef T&amp; reference;
    typedef const T* const_pointer;
    typedef const T&amp; const_reference;
    template &lt;class U&gt; struct rebind { typedef MyAlloc&lt;U&gt; other; };
    MyAlloc() {}
    MyAlloc( const MyAlloc&amp; other ) {}
    T* allocate( std::size_t n ) { return static_cast&lt;T*&gt;( std::malloc( n * sizeof( value_type ) ) ); }
    void deallocate( T* p, std::size_t n ) { std::free( p ); }
  };
  
  int main( int argc, char* argv[] )
  {
    std::map&lt;myuuid, int, std::less&lt;myuuid&gt;, MyAlloc&lt;myuuid&gt; &gt; map;
    myuuid myu;
    map[ myu ] = 5;
  
    return 0;
  }
  </code></pre>
  
  <p>Momentarily ignoring the custom allocator, if I correctly understand the linked answers, the reason that <code>std::map&lt;myuuid, int&gt; map; myuuid myu; map[myu] = 5;</code> fails comes down to the following being impossible:</p>
  
  <pre><code>int main( int argc, char* argv[] )
  {
    char a[3];
    char b[3];
    b = a; // Illegal - won't compile
    return 0;
  }
  </code></pre>
  
  <p><strong>Question</strong>:</p>
  
  <p>I understand why the above is illegal - but am I correct that this demonstrates the reason why <code>std::map&lt;myuuid, int&gt; map; myuuid myu; map[myu] = 5;</code> is illegal?</p>
  
  <p><strong>Question</strong>:</p>
  
  <p>I thought I might be able to get away with compiling <code>std::map&lt;myuuid, int&gt; map; myuuid myu; map[myu] = 5;</code> if I implemented a custom allocator. I guessed that perhaps the <code>=</code> (assignment) of a <code>myuuid</code> would be "rerouted" to <code>MyAlloc::allocate()</code>, but that was a wild, unreasoned guess, which seems to be false. Is there a way that a custom allocator can be modified to solve the first code-block's compilation error?</p>
  
  <p>I have a half-baked notion that that <code>operator=</code> on <code>myuuid</code> operands could be "rerouted" to the custom allocator, but I don't know if this is true for PODs (<code>myuuid</code> is just typedef'ed to a POD`).</p>
  
  <p>The compile error is too voluminous to post here, but tellingly, the first error is:</p>
  
  <pre><code>/usr/include/c++/4.8.3/bits/stl_pair.h: In instantiation of \u2018std::pair&lt;_T1, _T2&gt;::pair(const _T1&amp;, const _T2&amp;) [with _T1 = const char [16]; _T2 = int]\u2019:
  /usr/include/c++/4.8.3/bits/stl_map.h:469:59:   required from \u2018std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::mapped_type&amp; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::operator[](const key_type&amp;) [with _Key = char [16]; _Tp = int; _Compare = std::less&lt;char [16]&gt;; _Alloc = MyAlloc&lt;char 
  [16]&gt;; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::mapped_type = int; std::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::key_type = char [16]]\u2019
  main.cpp:27:12:   required from here
  /usr/include/c++/4.8.3/bits/stl_pair.h:113:31: error: array used as initializer
         : first(__a), second(__b) { }
  </code></pre>
  
  <p>Interestingly, <code>error: array used as initializer</code> is the original compile error I was trying to solve from before introducing the custom allocator. So this seems like a recursive problem.</p>
  
  <p><strong>Question</strong>:</p>
  
  <p>Can use of an array as a <code>std::map</code> key be pulled off somehow by making use of a custom allocator? (Perhaps there's an optional function I should implement?) Or are the alternatives at the noted links the only solutions? (It was not raised in the answers to those questions, but since custom allocators are a bit esoteric, I thought it was worth a separate question)</p>