简体   繁体   English

为什么不将reinterpret_cast转换为'unsigned char'为'char'?

[英]Why doesn't reinterpret_cast convert 'unsigned char' to 'char'?

I am trying to compile this library using MSVC10, and this function is giving me headache: 我正在尝试使用MSVC10编译这个库,这个函数让我很头疼:

/*! \brief Read bytes from a \c std::istream
    \param is The stream to be read.
    \param data A pointer to a location to store the bytes.
    \param size The number of bytes to be read.
*/
void _read(std::istream &is, unsigned char *data, int size)
{
    for (int i=0; i < size ; ++i )
      is.get(static_cast<char>(data[i]));
}

error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem &)' : cannot convert parameter 1 from 'char' to 'char &' 错误C2664:'std :: basic_istream <_Elem,_ Traits>&std :: basic_istream <_Elem,_Traits> :: get(_Elem&)':无法将参数1从'char'转换为'char&'

The original used static_cast, so I try with reinterpret_cast as suggested elsewhere but that fails too: 原来使用的是static_cast,所以我按照其他地方的建议尝试使用reinterpret_cast但是也失败了:

error C2440: 'reinterpret_cast' : cannot convert from 'unsigned char' to 'char' 错误C2440:'reinterpret_cast':无法从'unsigned char'转换为'char'

This library comes with unix makefiles. 该库附带unix makefile。 What is the best way to resolve this compile error? 解决此编译错误的最佳方法是什么?

Because reinterpret_cast does not work that way, by definition. 因为根据定义, reinterpret_cast不能以这种方式工作。

In order to perform memory re-interpretation, you have to apply reinterpret_cast to pointers or references. 为了执行内存重新解释,您必须将reinterpret_cast应用于指针或引用。 If you want to reinterpret unsigned char data as char data, you actually have to convert to char & type, not to char type. 如果要将unsigned char数据重新解释为char数据,则实际上必须转换为char & type,而不是char类型。

In your case that would be 在你的情况下,将是

is.get(reinterpret_cast<char &>(data[i]));

Or you can go the pointer route and do 或者你可以去指针路线做

is.get(*reinterpret_cast<char *>(&data[i]));

(which is the same thing). (这是一回事)。

Because you need a char& that is a reference to char but the result of the cast is an r-value, and so not bindable to the reference. 因为你需要一个char&并且它是对char的引用,但是转换的结果是一个r值,因此不能绑定到引用。

You need something like: 你需要这样的东西:

is.get(reinterpret_cast<char&>(data[i]));

But in this particular case you can / should use static_cast<char&> : 但在这种特殊情况下,您可以/应该使用 static_cast<char&>

 
 
 
 
  
  
  is.get(static_cast<char&>(data[i]));
 
 
  

Try this instead: 试试这个:

void _read(std::istream &is, unsigned char *data, int size)
{
    for (int i=0; i < size ; ++i )
      is.get(reinterpret_cast<char*>(data)[i]);
}

In addition to the other answers which cope with the casting problem: 除了应对铸造问题的其他答案:

Why don't you just use istream::read to read size bytes at once? 为什么不直接使用istream::read读取size字节? This avoids your hand-crafted for-loop and should be faster. 这样可以避免手工制作的for循环,并且应该更快。

void _read(std::istream &is, unsigned char *data, int size) {
    is.read(reinterpret_cast<char*>(data), size);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM