g ++ 4.4.5
我有一个扩展类std :: ofstream的类来添加一些功能.
MyStream& MyStream::operator<<(const bool& val) {
if(this->pos == 8) {
this->pos = 0;
ofstream::operator<<(this->curbyte); //call the parent method
}
curbyte = curbyte + (val << pos++);
return *(this);
}
Run Code Online (Sandbox Code Playgroud)
这基本上允许你将单个位写为bool然后它将使用父<<方法写入每组8.我不得不在这里使用这个调用语法,因为我正在调用基本方法,但在我使用这个类的实际main方法中,我尝试调用以下行:
bout << (unsigned char) 255u;
Run Code Online (Sandbox Code Playgroud)
我想要调用<<方法已经为ofstream和unsigned char定义了但是它给了我一个很长的模糊的重载错误,列出了已经为ofstream定义的所有char相关的候选者(char,unsigned char,signed char)和我自己的bool方法,即使我明确地转向char.但是我确实设法让它与以下工作:
bout.operator<<((unsigned char) 255u);
Run Code Online (Sandbox Code Playgroud)
这必须与g ++如何进行隐式转换有关(我的猜测是在第一种情况下我的用户定义的转换之后还有一次可能的转换,这使得函数调用语法避免不明确).有没有人确切知道为什么会发生这种情况,或者是否有更好的语法来避免错误?