Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Bool在'std::rel_ops'示例中强制转换?_C++_C++11 - Fatal编程技术网

C++ Bool在'std::rel_ops'示例中强制转换?

C++ Bool在'std::rel_ops'示例中强制转换?,c++,c++11,C++,C++11,考虑一下这里的std::rel_ops示例: #包括 #包括 结构Foo{ int n; }; 布尔运算符==(常数Foo和lhs、常数Foo和rhs) { 返回lhs.n==rhs.n; } bool操作符是,对bool的强制转换在这里是多余的。我已经冒昧地从文档中删除了它们 #include <iostream> #include <utility> struct Foo { int n; }; bool operator==(const Foo&

考虑一下这里的
std::rel_ops
示例:

#包括
#包括
结构Foo{
int n;
};
布尔运算符==(常数Foo和lhs、常数Foo和rhs)
{
返回lhs.n==rhs.n;
}

bool操作符是,对
bool
的强制转换在这里是多余的。我已经冒昧地从文档中删除了它们

#include <iostream>
#include <utility>

struct Foo {
    int n;
};

bool operator==(const Foo& lhs, const Foo& rhs)
{
    return lhs.n == rhs.n;
}

bool operator<(const Foo& lhs, const Foo& rhs)
{
    return lhs.n < rhs.n;
}

int main()
{
    Foo f1 = {1};
    Foo f2 = {2};
    using namespace std::rel_ops;

    std::cout << std::boolalpha;
    std::cout << "not equal?     : " << (bool) (f1 != f2) << '\n';
    std::cout << "greater?       : " << (bool) (f1 > f2) << '\n';
    std::cout << "less equal?    : " << (bool) (f1 <= f2) << '\n';
    std::cout << "greater equal? : " << (bool) (f1 >= f2) << '\n';
}