Bool在`std :: rel_ops`示例中强制转换?

时间:2012-12-17 16:32:20

标签: c++ c++11

请考虑此处的std::rel_ops示例:

http://en.cppreference.com/w/cpp/utility/rel_ops/operator_cmp

#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';
}

(bool)演员的目的是什么? (f1 != f2)类型bool已经不是{{1}}吗?

1 个答案:

答案 0 :(得分:2)

是的,bool的演员阵容在这里是多余的。我冒昧地将它们从文档中删除。