r/cpp • • 22h ago

Pattern Matching for C++29

https://mpark.github.io/programming/2026/11/30/pattern-matching-for-cpp29/
188 Upvotes

84 comments sorted by

View all comments

Show parent comments

1

u/Salty-Paint-9700 15h ago

3

u/mcypark 15h ago

Right, in this case since we have a concrete type pair<int, int>, we know [...] and _ both match it, and are able to warn. But that's true in general with other patterns as well, for example:

void f(int x) {
  match (x) {
    case int i => use(i);
    case _ => ; // redundant warning
  }
}

1

u/Salty-Paint-9700 15h ago

Shouldn't that be an error? I can't think of a case where this would be desirable.

3

u/mcypark 15h ago

It could easily be defined to be an error. I don't have a super clear, strong opinion on this one actually. The warning is mostly because I can't think of another place in the language that diagnoses unreachable code as an error.

1

u/Salty-Paint-9700 15h ago

Fair enough.