r/cpp • • 1d ago

Pattern Matching for C++29

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

90 comments sorted by

View all comments

Show parent comment

4

u/mcypark 15h ago edited 15h ago

Right, I did consider that use case as well. Just to make sure I'm clearly understanding, and for others to follow along as well since it's a bit subtle. If we have an enum like:

enum class Color { Red, Green, Blue };

The proposal considers this exhaustive:

void f(Color color) {
  match (color) {
    case Color::Red => ;
    case Color::Green => ;
    case Color::Blue => ;
  }
}

Of course this is not really exhaustive since static_cast<Color>(3) is a valid value, but it's not covered here. You could cover it by adding a case _ => ; like:

void f(Color color) {
  match (color) {
    case Color::Red => ;
    case Color::Green => ;
    case Color::Blue => ;
    case _ => ;
  }
}

The problem is that now if we add Yellow as a new color, we don't get any diagnostic about "missing Yellow" since case _ covers it.

So what we want in this situation is to have a fallback case _, but make it such that it doesn't contribute to exhaustiveness checking, such that we still get a diagnostic for "missing Yellow" (This is the behavior we get from -Wswitch-enum).

In the proposal, a pattern that has an if guard does not contribute to exhaustiveness checking. As such, we can leverage this to achieve this by writing:

void f(Color color) {
  match (color) {
    case Color::Red => ;
    case Color::Green => ;
    case Color::Blue => ;
    case _ if (true) => ;
//  ^^^^^^^^^^^^^^^^ catch-all, but don't contribute to exhaustiveness
  }
}

An alternative to this weird spelling is to spell it default:

void f(Color color) {
  match (color) {
    case Color::Red => ;
    case Color::Green => ;
    case Color::Blue => ;
    default => ;
//  ^^^^^^^ catch-all, but don't contribute to exhaustiveness
  }
}

While I did like the spelling of default better than case _ if (true), I felt that having to explain all of this as a subtle difference between case _ and default was a bit too much. Perhaps my explanation was longer than necessary, but in my experience describing this to folks, it usually takes a while before they're even willing to listen to an explanation as to why case _ and default would be at all different 😅

2

u/Salty-Paint-9700 14h ago

case _ if (true) is not really intuitive and sounds like a hack more than anything, but perhaps this is a problem with enums and not with patterns. If there was a standard facility to check if enum value is one of the named ones this could maybe become something like
case _ if (!std::is_enum_v<Color>(color)) =>

That would technically solve this problem and at least said what it did, but is also wordy as hell and I can't imagine that check function being very performant (basically check for all known values equality, which could be a lot). Also that would be a whole different proposal unrelated to patterns.

3

u/mcypark 14h ago

Yeah, I don't particularly like it either. I do wonder if it could be helped by something simple like:

 inline constexpr bool is_residual = true;

and people would write like:

case _ if (std::is_residual) => ;

Since the values outside of the "normal" domain of an alternative type is called "residual" values.

That actually reads fairly well, ignoring the mechanism used to achieve it 😄

1

u/Salty-Paint-9700 14h ago

Now that's a hack with a lipstick 😂

2

u/mcypark 14h ago

I mean... 🤷🏻‍♂️ haha. What I will say is that case _ if (true) isn't introducing anything new. These parts need to exist independently regardless, so it's "nice" that they fit to serve this purpose. If/when there's a better, specific thing we want to introduce (e.g. default or something else), then that can be added without having to pull out stuff we don't want or something like that.