Попытка использовать uint * & как const unit * & терпит неудачу: недопустимая инициализация ссылки типа const uint8_t * & из выражения типа uint8_t *
Следующий код не может быть скомпилирован для меня (gcc 4.6.3, Ubuntu 12.04):
#include
#include
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(buf) = value;
adjustBuffer(buf, bufSize, valueSize);
return valueSize;
}
bool unpackInt(const uint8_t *&buf, size_t &bufSize, int &value)
{
value = *reinterpret_cast(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 * &) к константе uint8_t * & (который IIRC является ссылкой на указатель на const). Во-первых, я неЯ не понимаю, почему он пытается назначить указатель на uint8_t, а не ссылку на указатель. Во-вторых, не должент конверсионная работа? Вы можете конвертировать uint8_t * в const uint8_t *, почему быт преобразование ссылок на оба типа работы?
Конечно, добавляя AdjustBuffer (), который принимает const uint8_t * & работает, но яхотелось бы понять почему