Discussion about this post

User's avatar
Han Jiang's avatar

"The Problem" indeed exists in real-word code, but imo it's caused by bad coding styles. Using `bool operator==(element_t, std::uint32_t)` is a temporary solution and causes confusion to readers including the future author himself.

I would rather keep my code "clean":

1. Make projection (loss of information) explicit, since an object is-not-equal-to its member (in information theory sense);

2. Avoid comparing values of different types.

```

std::ranges::find(elements, what_to_find, &element_t::member);

std::ranges::find(ints, static_cast<int>(a_long)); // value truncated, dangerous

std::ranges::find(ints | std::transform(turn_int_to_long), a_long); // value preserved, safe

std::ranges::find(longs, static_cast<long>(a_int)); // elements preserved, safe

std::ranges::find(longs | std::transform(turn_long_to_int), a_int); // elements truncated, dangerous

```

Expand full comment
1 more comment...

No posts

Ready for more?