{"id":4203,"date":"2021-03-18T22:50:49","date_gmt":"2021-03-18T20:50:49","guid":{"rendered":"https:\/\/mariusbancila.ro\/blog\/?p=4203"},"modified":"2021-03-19T12:54:45","modified_gmt":"2021-03-19T10:54:45","slug":"express-one-of-multiple-options-in-a-nice-way","status":"publish","type":"post","link":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/","title":{"rendered":"Express one of multiple options in a nice way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">We often find ourselves writing if statements where a variable is compared with several values either to check if it matches one of them or that it doesn\u2019t match any. Here is an example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">int option = ...;\n\n\/\/ at least a value is matched\nif (option == 12 || option == 23 || option == 42)\n{\n   std::cout &lt;&lt; \"it's a good option\\n\";\n}\n\n\/\/ no value is matched\nif (option != 12 &amp;&amp; option != 23 &amp;&amp; option != 42)\n{\n   std::cout &lt;&lt; \"it's a bad option\\n\";\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This example has three comparison values for each case, but it could be 5, or 10, or any number. If it\u2019s too many then perhaps a different approach should be taken. However, the question is, how do we express this in a simpler way in C++, rather than a long if condition?<br><!-- more --><br>Before we continue, please ignore the magic numbers used here. They are just some values to show the point in a simple manner. In real code, you should use enums, or constexpr values, or (evil option) macros. Here is a real example from some code I\u2019m working with:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (iCtrlType != CTRLTYPE_BUTTON &amp;&amp;\n    iCtrlType != CTRLTYPE_CHECKBOX &amp;&amp;\n    iCtrlType != CTRLTYPE_COMBOBOX &amp;&amp;\n    iCtrlType != CTRLTYPE_COMBOGRID &amp;&amp;\n    iCtrlType != CTRLTYPE_DROPDOWNCAL &amp;&amp;\n    iCtrlType != CTRLTYPE_EDIT)\n{\n   \/\/ do something\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In SQL, we can write statements of the following forms:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT column-names\nFROM table-name\nWHERE column-name IN (values) <\/pre>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">SELECT column-names\nFROM table-name\nWHERE column-name NOT IN (values)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Can we achieve something similar to the SQL <code>IN<\/code> operator in C++? Well, in C++11 we have <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/all_any_none_of\" target=\"_blank\" rel=\"noopener noreferrer\"><code>std::all_of<\/code>, <code>std::any_of<\/code>, <code>std::none_of<\/code><\/a> that can help express the same intent. Using <code>any_of<\/code> and <code>none_of<\/code> we can rewrite the two if statements above as following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;int> good_options{ 12, 23, 42 };\nif (std::any_of(good_options.begin(), good_options.end(), [option](int const o) { return option == o; }))\n{\n   std::cout &lt;&lt; \"it's a good option\\n\";\n}\n\nif (std::none_of(good_options.begin(), good_options.end(), [option](int const o) { return option == o; }))\n{\n   std::cout &lt;&lt; \"it's a bad option\\n\";\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is very verbose, and personally, I don\u2019t like it at all. I don\u2019t like it because you need to put the values in a container (a <code>vector<\/code> here) and you need to supply iterators to the beginning and the end of the container, as well as a lambda to compare your value to every value in the container. There are simpler ways to do this.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In C++20, we have ranges, and the ranges library provides a new implementation for these algorithms, called <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/ranges\/all_any_none_of\" target=\"_blank\" rel=\"noopener noreferrer\"><code>std::ranges::all_of<\/code>, <code>std::ranges::any_of<\/code>, <code>std::ranges::none_of<\/code><\/a>. Using these algorithms, we can simply supply a range (such as a container) instead of the pair of begin-end iterators. Therefore, our code would look as the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">std::vector&lt;int> good_options{ 12, 23, 42 };\n\nif (std::ranges::any_of(good_options, [option](int const o) { return option == o; }))\n{\n   std::cout &lt;&lt; \"it's a good option\\n\";\n}\n\nif (std::ranges::none_of(good_options, [option](int const o) { return option == o; }))\n{\n   std::cout &lt;&lt; \"it's a bad option\\n\";\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is simpler, but I\u2019m still not satisfied. We still need to put the values in a container. It\u2019s not possible to simplify the code as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (std::ranges::any_of({ 12, 23, 42 }, [option](int const o) { return option == o; }))\n{\n   std::cout &lt;&lt; \"it's a good option\\n\";\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s closer to the code I would like to be able to write. That code is shown below:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (any_of({ 12, 23, 42 }, option))\n{\n   std::cout &lt;&lt; \"it's a good option\\n\";\n}\n\nif (none_of({ 12, 23, 42 }, option))\n{\n    std::cout &lt;&lt; \"it's a bad option\\n\";\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is an in-place sequence of values and a variable, instead of a lambda. This does not work out of the box with any standard algorithm, but we can easily write this <code>any_of<\/code> and <code>none_of<\/code> ourselves. A possible implementation is listed in the following snippet:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T>\nbool any_of(std::initializer_list&lt;T> r, T const v)\n{\n   return std::any_of(r.begin(), r.end(), [v](int const x) { return v == x; });\n}\n\ntemplate &lt;typename T>\nbool none_of(std::initializer_list&lt;T> r, T const v)\n{\n   return std::none_of(r.begin(), r.end(), [v](int const x) { return v == x; });\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">I know people will say that <code>std::initializer_list<\/code> is not a container and should not be used as such, but I don\u2019t really feel it is used as one in this snippet. It basically holds a temporary sequence of values that are iterated with the <code>std::any_of<\/code> and <code>std::none_of_<\/code> algorithms. This implementation will enable us to write code as in the previous snippet.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, there is one more simplification that we could think of, which is shown here:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">if (any_of(option, 12, 23, 42))\n{\n   std::cout &lt;&lt; \"it's a good option\\n\";\n}\n\nif (none_of(option, 12, 23, 42))\n{\n   std::cout &lt;&lt; \"it's a bad option\\n\";\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There is no sequence of values, just a variable number of arguments, passed to a function. That means, this time, the implementation of <code>any_of<\/code> and <code>none_of<\/code> should be based on variadic templates. The way I wrote it, using fold expressions, is as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T, typename ...Args>\nbool any_of(T const v, Args&amp;&amp;... args)\n{\n   return ((args == v) || ...);\n}\n\ntemplate &lt;typename T, typename ...Args>\nbool none_of(T const v, Args&amp;&amp;... args)\n{\n   return ((args != v) &amp;&amp; ...);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This time, the variable is supplied as the first argument, and the values to test against follow it. Unfortunately, this implementation allows calls without any values to compare to, such as <code>any_of(option)<\/code>. However, that is relatively simple to avoid by adding a <code>static_assert<\/code> statement, as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T, typename ...Args>\nbool any_of(T const v, Args&amp;&amp;... args)\n{\n   static_assert(sizeof...(args) > 0, \"You need to supply at least one argument.\");\n   return ((args == v) || ...);\n}\n\ntemplate &lt;typename T, typename ...Args>\nbool none_of(T const v, Args&amp;&amp;... args)\n{\n   static_assert(sizeof...(args) > 0, \"You need to supply at least one argument.\");\n   return ((args != v) &amp;&amp; ...);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If you don\u2019t like <code>static_assert<\/code>s and you are using C++20, then you can use constraints to require the parameter pack to have at least one element. The change is relatively simple and looks as the following:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">template &lt;typename T, typename ...Args>\nbool any_of(T const v, Args&amp;&amp;... args) requires (sizeof...(args) > 0)\n{\n   return ((args == v) || ...);\n}\n\ntemplate &lt;typename T, typename ...Args>\nbool none_of(T const v, Args&amp;&amp;... args) requires (sizeof...(args) > 0)\n{\n   return ((args != v) &amp;&amp; ...);\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, C++ offers different standard and DIY ways of replacing some category of if statements with a function call.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We often find ourselves writing if statements where a variable is compared with several values either to check if it matches one of them or that it doesn&#8217;t match any. Here is an example:<\/p>\n<pre class=\"lang:c++ decode:true \" >int option = ...;\r\n\r\n\/\/ at lease a value is matched\r\nif (option == 12 || option == 23 || option == 42)\r\n{\r\n   std::cout &lt;&lt; \"it's a good option\\n\";\r\n}\r\n\r\n\/\/ no value is matched\r\nif (option != 12 &amp;&amp; option != 23 &amp;&amp; option != 42)\r\n{\r\n   std::cout &lt;&lt; \"it's a bad option\\n\";\r\n}<\/pre>\n<p>This example has three comparison values for each case, but it could be 5, or 10, or any number. If it&#8217;s too many then perhaps a different approach should be taken. However, the question is, how do we express this in a simpler way in C++, rather than a long if condition?<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","advgb_blocks_editor_width":"","advgb_blocks_columns_visual_guide":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[7],"tags":[451,543,635,579],"class_list":["post-4203","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-c20","tag-constraints","tag-ranges"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.3 - aioseo.com -->\n\t<meta name=\"description\" content=\"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code.\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Marius Bancila\"\/>\n\t<meta name=\"keywords\" content=\"c++,ranges,constraints,c++20\" \/>\n\t<link rel=\"canonical\" href=\"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/\" \/>\n\t\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.3\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"Marius Bancila&#039;s Blog | About code. Mostly on C++\" \/>\n\t\t<meta property=\"og:type\" content=\"article\" \/>\n\t\t<meta property=\"og:title\" content=\"Express one of multiple options in a nice way\" \/>\n\t\t<meta property=\"og:description\" content=\"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code.\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/\" \/>\n\t\t<meta property=\"article:published_time\" content=\"2021-03-18T20:50:49+00:00\" \/>\n\t\t<meta property=\"article:modified_time\" content=\"2021-03-19T10:54:45+00:00\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Express one of multiple options in a nice way\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code.\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#article\",\"name\":\"Express one of multiple options in a nice way\",\"headline\":\"Express one of multiple options in a nice way\",\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"},\"datePublished\":\"2021-03-18T22:50:49+02:00\",\"dateModified\":\"2021-03-19T12:54:45+02:00\",\"inLanguage\":\"en-US\",\"commentCount\":3,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#webpage\"},\"articleSection\":\"C++, C++, c++20, constraints, ranges\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"position\":2,\"name\":\"IT\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"position\":3,\"name\":\"Software\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/#listItem\",\"name\":\"IT\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"position\":4,\"name\":\"C++\",\"item\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#listItem\",\"name\":\"Express one of multiple options in a nice way\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/#listItem\",\"name\":\"Software\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#listItem\",\"position\":5,\"name\":\"Express one of multiple options in a nice way\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/category\\\/it\\\/software\\\/c\\\/#listItem\",\"name\":\"C++\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/\",\"name\":\"Marius Bancila\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Marius Bancila\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#webpage\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/\",\"name\":\"Express one of multiple options in a nice way\",\"description\":\"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code.\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/2021\\\/03\\\/18\\\/express-one-of-multiple-options-in-a-nice-way\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/author\\\/admin\\\/#author\"},\"datePublished\":\"2021-03-18T22:50:49+02:00\",\"dateModified\":\"2021-03-19T12:54:45+02:00\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/\",\"name\":\"Marius Bancila's Blog\",\"description\":\"About code. Mostly on C++\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/mariusbancila.ro\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Express one of multiple options in a nice way","description":"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code.","canonical_url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/","robots":"max-image-preview:large","keywords":"c++,ranges,constraints,c++20","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#article","name":"Express one of multiple options in a nice way","headline":"Express one of multiple options in a nice way","author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"},"datePublished":"2021-03-18T22:50:49+02:00","dateModified":"2021-03-19T12:54:45+02:00","inLanguage":"en-US","commentCount":3,"mainEntityOfPage":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#webpage"},"isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#webpage"},"articleSection":"C++, C++, c++20, constraints, ranges"},{"@type":"BreadcrumbList","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","position":1,"name":"Home","item":"https:\/\/mariusbancila.ro\/blog\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","position":2,"name":"IT","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","position":3,"name":"Software","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/#listItem","name":"IT"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","position":4,"name":"C++","item":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/","nextItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#listItem","name":"Express one of multiple options in a nice way"},"previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/#listItem","name":"Software"}},{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#listItem","position":5,"name":"Express one of multiple options in a nice way","previousItem":{"@type":"ListItem","@id":"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/#listItem","name":"C++"}}]},{"@type":"Organization","@id":"https:\/\/mariusbancila.ro\/blog\/#organization","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","url":"https:\/\/mariusbancila.ro\/blog\/"},{"@type":"Person","@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author","url":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/","name":"Marius Bancila","image":{"@type":"ImageObject","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/a84dd2831d955c38355ddea55df4df260809b88f36408bc14fd4eab8f7f131c9?s=96&d=mm&r=g","width":96,"height":96,"caption":"Marius Bancila"}},{"@type":"WebPage","@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#webpage","url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/","name":"Express one of multiple options in a nice way","description":"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code.","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/mariusbancila.ro\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/#breadcrumblist"},"author":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"creator":{"@id":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/#author"},"datePublished":"2021-03-18T22:50:49+02:00","dateModified":"2021-03-19T12:54:45+02:00"},{"@type":"WebSite","@id":"https:\/\/mariusbancila.ro\/blog\/#website","url":"https:\/\/mariusbancila.ro\/blog\/","name":"Marius Bancila's Blog","description":"About code. Mostly on C++","inLanguage":"en-US","publisher":{"@id":"https:\/\/mariusbancila.ro\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"Marius Bancila's Blog | About code. Mostly on C++","og:type":"article","og:title":"Express one of multiple options in a nice way","og:description":"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code.","og:url":"https:\/\/mariusbancila.ro\/blog\/2021\/03\/18\/express-one-of-multiple-options-in-a-nice-way\/","article:published_time":"2021-03-18T20:50:49+00:00","article:modified_time":"2021-03-19T10:54:45+00:00","twitter:card":"summary","twitter:title":"Express one of multiple options in a nice way","twitter:description":"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code."},"aioseo_meta_data":{"post_id":"4203","title":null,"description":"Explore different ways to replace if statements that test a variable against different values with a function call to simplify your C++ code.","keywords":[{"label":"C++","value":"C++"},{"label":"ranges","value":"ranges"},{"label":"constraints","value":"constraints"},{"label":"C++20","value":"C++20"}],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"aioseo-article-636965220c0ac","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","location":null,"local_seo":{"businessInfo":{"name":"","urls":{"website":"","aboutPage":"","contactPage":""},"address":{"line1":"","line2":"","zip":"","city":"","state":"","country":""},"contact":{"email":"","phone":"","fax":""},"ids":{"vatID":"","taxID":"","chamberID":""},"payment":{"priceIndication":"","currenciesAccepted":"","methodsAccepted":""},"areaServed":""},"openingHours":{"show":false,"closedLabel":"","open24h":false,"open24hLabel":"","open247":false,"use24hFormat":false,"twoSets":false,"timezone":"","hours":[]}},"breadcrumb_settings":null,"limit_modified_date":false,"ai":null,"created":"2021-03-18 21:02:54","updated":"2025-12-12 07:56:25","seo_analyzer_scan_date":null,"focus_keyword":null,"additional_keywords":null,"truseo_locale":null},"author_meta":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"featured_img":null,"jetpack_publicize_connections":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Marius Bancila","author_link":"https:\/\/mariusbancila.ro\/blog\/author\/admin\/"},"uagb_comment_info":3,"uagb_excerpt":"We often find ourselves writing if statements where a variable is compared with several values either to check if it matches one of them or that it doesn't match any. Here is an example: int option = ...; \/\/ at lease a value is matched if (option == 12 || option == 23 || option&hellip;","coauthors":[],"tax_additional":{"categories":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>"]},"tags":{"linked":["<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">C++<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">c++20<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">constraints<\/a>","<a href=\"https:\/\/mariusbancila.ro\/blog\/category\/it\/software\/c\/\" class=\"advgb-post-tax-term\">ranges<\/a>"],"unlinked":["<span class=\"advgb-post-tax-term\">C++<\/span>","<span class=\"advgb-post-tax-term\">c++20<\/span>","<span class=\"advgb-post-tax-term\">constraints<\/span>","<span class=\"advgb-post-tax-term\">ranges<\/span>"]}},"comment_count":"3","relative_dates":{"created":"Posted 6 years ago","modified":"Updated 6 years ago"},"absolute_dates":{"created":"Posted on March 18, 2021","modified":"Updated on March 19, 2021"},"absolute_dates_time":{"created":"Posted on March 18, 2021 10:50 pm","modified":"Updated on March 19, 2021 12:54 pm"},"featured_img_caption":"","series_order":"","jetpack_shortlink":"https:\/\/wp.me\/pYNdv-15N","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/comments?post=4203"}],"version-history":[{"count":22,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4203\/revisions"}],"predecessor-version":[{"id":4235,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/posts\/4203\/revisions\/4235"}],"wp:attachment":[{"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/media?parent=4203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/categories?post=4203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mariusbancila.ro\/blog\/wp-json\/wp\/v2\/tags?post=4203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}