C++比较表达式bug

era*_*ros 1 c++

这段代码有问题,但我找不到导致它的原因.

bool Parser::validateName(std::string name) {
    int pos = name.find(INVALID_CHARS);               //pos is -1, 
    bool result = ((name.find(INVALID_CHARS)) < 0);   //result is false
    //That was weird, does that imply that -1 >= 0?, let's see
    result = (pos < 0)                                //result is true
    result = ((name.find(INVALID_CHARS)) == -1)       //result is true
    result = (-1 < 0)                                 //result is true
    ...
}
Run Code Online (Sandbox Code Playgroud)

为什么第二行的结果为false.有什么我没看到的吗?

Pla*_*aHH 9

std::string::find返回std::string::npos的类型std::string::size_type定义为实现定义的无符号整数.无符号整数永远不会小于0.

你应该总是比较,std::string::npos以检查是否std::string::find找到了什么.