次のコードはコンパイルに失敗します(gcc 4.6.3、Ubuntu 12.04):
#include <inttypes.h>
#include <stdio.h>
static inline void adjustBuffer(const uint8_t *&buf, size_t &bufSize, size_t len)
{
buf += len;
bufSize -= len;
}
uint16_t packInt(uint8_t *&buf, size_t &bufSize, int value)
{
size_t valueSize = sizeof(int);
*reinterpret_cast<int *>(buf) = value;
adjustBuffer(buf, bufSize, valueSize);
return valueSize;
}
bool unpackInt(const uint8_t *&buf, size_t &bufSize, int &value)
{
value = *reinterpret_cast<const int*>(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;
}
次のエラーが発生します。
$ 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*&, size_t&, int)’:
ref_to_ptr.cpp:15:41: error: invalid initialization of reference of type ‘const uint8_t*& {aka const unsigned char*&}’ 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*&, size_t&, size_t)’
ref_to_ptr.cpp: In function ‘bool unpackInt(const uint8_t*&, size_t&, int&)’:
ref_to_ptr.cpp:22:29: error: invalid initialization of non-const reference of type ‘const uint8_t*& {aka const unsigned char*&}’ from an rvalue of type ‘unsigned int’
ref_to_ptr.cpp:5:20: error: in passing argument 1 of ‘void adjustBuffer(const uint8_t*&, size_t&, 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*& {aka const unsigned char*&}’ 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*&, size_t&, int&)’
make: *** [ref_to_ptr] Error 1
コンパイラは、uint8_t *(uint8_t *&)への参照をconst uint8_t *&(IIRCはconstへのポインタへの参照)に割り当てるのに問題があるようです。まず、ポインタへの参照ではなく、uint8_tにポインタを割り当てようとする理由がわかりません。第二に、変換は機能しないのですか?uint8_t*をconstuint8_t*に変換できますが、両方のタイプへの参照の変換が機能しないのはなぜですか?
もちろん、const uint8_t *&を取るadjustBuffer()を追加することはできますが、その理由を理解したいと思います