Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bool casts in `std::rel_ops` example?

Tags:

c++

c++11

Consider the std::rel_ops example here:

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

What is the purpose of the (bool) casts? Isn't (f1 != f2) already of type bool?

like image 814
Andrew Tomazos Avatar asked Jan 21 '26 11:01

Andrew Tomazos


1 Answers

Yes, the casts to bool are redundant here. I’ve taken the liberty of removing them from the documentation.

like image 139
Konrad Rudolph Avatar answered Jan 24 '26 07:01

Konrad Rudolph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!