Skip to content

yurablok/switch-str

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

switch-str

C++17 O(1) switch for strings.

Benefits and features:

  • O(1) cases matching.
  • Faster than if-elseif-elseif-else.
  • break, continue, return, [[fallthrough]] work as expected.
  • Compile-time errors for repetitive and unlisted cases.

Example of usage:

#include <switch_str.hpp>

void test(const std::string_view value) {
    switch_str(value,
        "ERR", "MSH", "OBR", "PID") {
    case_str("ERR"):
        ...
        break;
    case_str("MSH"):
        static_assert(switch_str_meta::cases().size() == 4);
        static_assert(switch_str_meta::cases()[1] == "MSH");
        break;
    case_str("PID"):
        ...
        break;
    case_str("PID"): // Repetition -> compile-time error.
        ...
        break;
    case_str("PV1"): // Unlisted -> compile-time error.
        ...
        break;
    default:
        ...
        break;
    }
}

Minimum compiler versions:

C++17 C++20
Clang 5.0 10.0
GCC 7.3 10.1
MSVC 19.15(15.8) 19.32(17.2)

Releases

No releases published

Packages

No packages published

Languages