将false转换为指针类型void*?

for*_*818 10 c++ boolean stream type-conversion

有人能解释我这里发生了什么......?

我有这个代码:

#include <fstream>
#include <string>
#include <iostream> 
int main(){   
    std::ifstream file("test.txt");    
    std::string x;
    while (true) { 
        if (!(file >> x)) return 0;
        std::cout << x << "\n";   
    }
}
Run Code Online (Sandbox Code Playgroud)

...编译好,做它应该做的,到目前为止没问题.有时候,我不喜欢!这么多,因为它可以很容易被忽略,所以我取代了if

if ((file >> x)==false) return 0;  
Run Code Online (Sandbox Code Playgroud)

..突然我的编译器(gcc 4.8.5)抱怨警告:

 warning: converting ‘false’ to pointer type ‘void*’ [-Wconversion-null]
     if ((file >> x)==false) return 0;
Run Code Online (Sandbox Code Playgroud)

这就是我开始感到困惑的地方.哪里void*来的?不>>返回应该转换为的引用bool吗?为什么false转换成void*?为什么在我不明确写的时候会触发相同的警告false

出于好奇,我也尝试了这个:

if ((file>>x)==true) return 0;
Run Code Online (Sandbox Code Playgroud)

从而引发一场错误风暴

error: no match for ‘operator==’ (operand types are ‘std::basic_istream<char>’ and ‘bool’)
 if ((file>>x)==true) return 0;
              ^
Run Code Online (Sandbox Code Playgroud)

现在我完全迷失了.如何是false一个不同的充booltrue?当然不同的价值观,但我一直认为true并且false属于同一类型.

Oli*_*rth 7

回想一下,C++有运算符重载.特别是std::basic_istream 超载operator!.

唉,运算符重载在语义上并不一致,因此a和a ==之间没有重载.因此比较失败.但是,也允许编译器应用隐式转换以使表达式编译 - 在这种情况下可能会隐式转换为空指针,并且具有重载(尽管显然已在C++ 11中替换- prescarmbly修复不一致).istreambooltruefalsebasic_istreamoperator void*operator bool