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