[#FOLLY_EXHAUSTIVE_SWITCH]
= FOLLY&lowbar;EXHAUSTIVE&lowbar;SWITCH
:mrdocs:

C&plus;&plus; switch&hyphen;case statements enable writing conditional statements that are checked for exhaustiveness by the compiler&period; However, C&plus;&plus; compilers don&apos;t enforce exhaustiveness very well&period; These macros enable you to explicitly opt into truly exhaustive switch&hyphen;cases, that are useful for scenarios where you may want to deal with enums that are inputs, rather than internal invariants&period;

== Synopsis

Declared in `&lt;folly&sol;lang&sol;Switch&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
#define FOLLY&lowbar;EXHAUSTIVE&lowbar;SWITCH(...)

----

== Description

Historically, we have encountered one too many instances of SEVs caused by improper handling of enums, so these macros are used to enable stronger, and more explicit guarantees when writing switch&hyphen;cases&period;

For example&colon;

enum class logposition&lowbar;t &lcub; sealed &equals; &hyphen;1, nonexistent &equals; &hyphen;2, &rcub;&semi;

void foo(logposition&lowbar;t logpos) &lcub; FOLLY&lowbar;EXHAUSTIVE&lowbar;SWITCH(&lcub; switch (logpos) &lcub; case logposition&lowbar;t&colon;&colon;sealed&colon; &sol;&sol; handle sealed case case logposition&lowbar;t&colon;&colon;nonexistent&colon; &sol;&sol; handle nonexistent case default&colon; &sol;&sol; handle non&hyphen;exceptional value &rcub; &rcub;)&semi; &rcub;

For the above, if you miss handling any case, or if you miss the default case, the compiler will complain&period;

When you switch on the value of a concretely defined enum with a very large set of values, and only need to address a sane subset of the values, you write a non&hyphen;exhaustive switch&period; Additionally, when you switch on the a non&hyphen;enum value (like an int), you write a non&hyphen;exhaustive switch&period;

enum class Color &lcub; &sol;&sol; every color in a 64 color palette named as an enum value &rcub;&semi;

bool isRedColor(Color c) &lcub; FOLLY&lowbar;NON&lowbar;EXHAUSTIVE&lowbar;SWITCH(&lcub; switch (c) &lcub; case Color&colon;&colon;Red&colon; case Color&colon;&colon;LightRed&colon; case Color&colon;&colon;DarkRed&colon; return true&semi; default&colon; return false&semi; &rcub; &rcub;)&semi; &rcub;

The above is the less common pattern for switch statements&period;

Note&colon; The two macros here do not work well before llvm&hyphen;18, they are known to work for most recent versions of gcc, however&period;


[.small]#Created with https://www.mrdocs.com[MrDocs]#
