Consider the following struct with a single data member and an operator==
struct S {
int a;
/*constexpr*/ bool operator==(const S& other) const {
return this->a == other.a;
}
};
in its use, two structs can easily be created as constexpr
with an initialization list
int main() {
constexpr S s1 = {1};
constexpr S s2 = {2};
constexpr bool b = s1 == s2; // error
return 0;
}
the bool comparison can't compile because the ==
operator is not marked as constexpr
, When it is, the program compiles.
Should all comparison operators for any class that can be constexpr
also be marked as constexpr
? I don't see any reason why not, but I also haven't seen code practicing this.
I would also take it a step further and ask if something like operator*(S, S)
should be constexpr
as well, all the time.