3 ms·
>The amazing part about examples like that is people read them, check that the compiler really does work on that basis, and then continue writing things in C++
by peppermint_gum 3y ago
>The amazing part about examples like that is people read them, check that the compiler really does work on that basis, and then continue writing things in C++ anyway. Wild.
Well, in modern C++ this code would look like this:
std::array<int, 4> table;
bool exists_in_table(int v)
{
for (auto &elem : table) {
if (elem == v) return true;
}
return false;
}
Or even simpler:
std::array<int, 4> table;
bool exists_in_table(int v)
{
return std::ranges::contains(table, v);
}
There's no shortage of footguns in C++, but nonetheless, modern C++ is safer than C.
- mike_hock 3y agoWeirdly, GCC fails to optimize this, but Clang does (if you make the table static as in the original example).