News for hackers. Stuff that matters.
Re:The C++ Iceberg by flohofwoe on Wednesday May 08, 2024 @04:36AM (#40295684)
Hmm, how would that look like? Like this?

    if ((val < min) || (val >= max)) {
        __builtin_unreachable();
    }
    switch (val) {
        ...
    }
I haven't actually tried that, but if it works as intended it would actually be better yeah (not really in the case where I'm using it, because I'm switching on an integer, not an enum).
Re:The C++ Iceberg by planede on Wednesday May 08, 2024 @05:22AM (#40295953)
More like:

  switch (e) {
    case A:
      foo(); break;
    case B:
      bar(); break;
  }
  std::unreachable();
instead of:

  switch (e) {
    case A:
      foo(); break;
    case B:
      bar(); break;
    default:
      std::unreachable();
  }
The former is more likely to produce a warning if there is an enumeration C that you forgot to handle, or you added an enumeration C and missed a switch-case to update.

edit:

duh, it's supposed to be returns here instead of breaks.

Re:The C++ Iceberg by flohofwoe on Wednesday May 08, 2024 @05:32AM (#40296010)
But then all case branches hit the std::unreachable. How can that work in practice?
Re:The C++ Iceberg by gravescale on Wednesday May 08, 2024 @06:13AM (#40296249)
I think it's supposed to be a return not a break.
Re:The C++ Iceberg by planede on Wednesday May 08, 2024 @06:16AM (#40296267)
yeah, thanks! I added an edit.
Re:The C++ Iceberg by H8crilA on Wednesday May 08, 2024 @06:03PM (#40303171)
This is so funny that you have just demonstrated how stupid this construction is in practice by making a mistake.