title: Trying to use a uint*& as a const unit*& fails: invalid initialization of reference of type ‘const uint8_t*&’ from expression of type ‘uint8_t*
body: <p>The following code fails to compile for me (gcc 4.6.3, Ubuntu 12.04): </p>
  
  <pre><code>#include &lt;inttypes.h&gt;
  #include &lt;stdio.h&gt;
  
  static inline void adjustBuffer(const uint8_t *&amp;buf, size_t &amp;bufSize, size_t len)
  {
      buf += len;
      bufSize -= len;
  }                
  
  uint16_t packInt(uint8_t *&amp;buf, size_t &amp;bufSize, int value)
  {
      size_t valueSize = sizeof(int);
      *reinterpret_cast&lt;int *&gt;(buf) = value;
      adjustBuffer(buf, bufSize, valueSize);
      return valueSize;
  }
  
  bool unpackInt(const uint8_t *&amp;buf, size_t &amp;bufSize, int &amp;value)
  {
      value = *reinterpret_cast&lt;const int*&gt;(buf);
      adjustBuffer(sizeof(int));
      return true;
  }
  
  int main()
  {
      static const size_t BufSize = 100;
      size_t bufSize = BufSize;
      uint8_t buf[BufSize];
      uint8_t *buf_ptr = buf;
      packInt(buf_ptr, bufSize, 1);
      bufSize = BufSize;
      int x;
      unpackInt(buf, bufSize, x);
      return 0;
  }
  </code></pre>
  
  <p>I get the following errors: </p>
  
  <pre><code>$ make CXXFLAGS="-Wall -g" ref_to_ptr
  g++ -Wall -g    ref_to_ptr.cpp   -o ref_to_ptr
  ref_to_ptr.cpp: In function ‘uint16_t packInt(uint8_t*&amp;, size_t&amp;, int)’:
  ref_to_ptr.cpp:15:41: error: invalid initialization of reference of type ‘const uint8_t*&amp; {aka const unsigned char*&amp;}’ from expression of type ‘uint8_t* {aka unsigned char*}’
  ref_to_ptr.cpp:5:20: error: in passing argument 1 of ‘void adjustBuffer(const uint8_t*&amp;, size_t&amp;, size_t)’
  ref_to_ptr.cpp: In function ‘bool unpackInt(const uint8_t*&amp;, size_t&amp;, int&amp;)’:
  ref_to_ptr.cpp:22:29: error: invalid initialization of non-const reference of type ‘const uint8_t*&amp; {aka const unsigned char*&amp;}’ from an rvalue of type ‘unsigned int’
  ref_to_ptr.cpp:5:20: error: in passing argument 1 of ‘void adjustBuffer(const uint8_t*&amp;, size_t&amp;, size_t)’
  ref_to_ptr.cpp: In function ‘int main()’:
  ref_to_ptr.cpp:35:30: error: invalid initialization of non-const reference of type ‘const uint8_t*&amp; {aka const unsigned char*&amp;}’ from an rvalue of type ‘uint8_t* {aka unsigned char*}’
  ref_to_ptr.cpp:19:6: error: in passing argument 1 of ‘bool unpackInt(const uint8_t*&amp;, size_t&amp;, int&amp;)’
  make: *** [ref_to_ptr] Error 1
  </code></pre>
  
  <p>It seems the compiler has trouble assigning a reference to uint8_t* (uint8_t *&amp;) to a const uint8_t *&amp; (which IIRC is a reference to pointer to const). First, I don't understand why it tries to assign a pointer to uint8_t and not a reference to a pointer. Second, shouldn't the conversion work? You can convert uint8_t * to const uint8_t *, why wouldn't converting references to both types work? </p>
  
  <p>Of course, adding an adjustBuffer() that takes a const uint8_t *&amp; works, but I'd like to understand why </p>