Matching
Default case match
enum Place{ california, washington, new_york, new_jersey, las_vegas };
int main()
{
const Place p = las_vegas;
bool executed = false;
(
Case( california ) { assert(
false ); },
Case( washington ) { assert(
false ); },
Case( new_york ) { assert(
false ); },
Case( new_jersey ) { assert(
false ); },
);
assert( executed );
}
Match by multiple parameters
enum Place{ california, washington, new_york, new_jersey, las_vegas };
class Parser{};
int main()
{
{
const Place p = california;
bool executed = false;
(
Case(
_1 == california &&
_2 ==
nullptr ) { assert(
false ); },
Case(
_1 == california &&
_2 !=
nullptr ) { executed =
true; }
);
assert( executed );
}
{
const Place p = california;
bool executed = false;
(
Case(
_1 == california ||
_2 ==
nullptr ) { executed =
true; },
Case(
_1 == california &&
_2 !=
nullptr ) { assert(
false ); }
);
assert( executed );
}
}
Out of order match
enum Place{ california, washington, new_york, new_jersey, las_vegas };
int main()
{
const Place p1 = california;
const Place p2 = washington;
const Place p3 = new_york;
{
bool executed = true;
(
Case(
_1 == california &&
_3 == new_york &&
_2 == washington ) { executed =
true; },
);
assert( executed );
}
{
bool executed = true;
(
Case(
_3 == new_york &&
_1 == california &&
_2 == washington ) { executed =
true; },
);
assert( executed );
}
{
bool executed = true;
(
Case(
_3 == new_york &&
_2 == washington &&
_1 == california ) { executed =
true; },
);
assert( executed );
}
}
Partial match
enum Place{ california, washington, new_york, new_jersey, las_vegas };
int main()
{
const Place p1 = california;
const Place p2 = washington;
const Place p3 = new_york;
(
Case(
_3 == new_york ) {
return true; },
Case(
_1 == california &&
_3 == new_york ) {
return false; },
Case(
_2 == washington &&
_3 == new_york ) {
return false; },
Case(
_1 == california &&
_2 == washington ) {
return false; },
Case(
_1 == california &&
_2 == washington &&
_3 == new_york ){
return false; },
Default { assert(
false );
return false; }
);
assert( status );
}
Mixed conditions
enum Place{ california, washington, new_york, new_jersey, las_vegas };
bool is_positive( const int i ) { return i > 0; }
bool is_odd( const int i ) { return i % 2 != 0; }
int main()
{
bool executed = false;
(
Case(
_1 != 0 && ( is_positive,
_1 ) && ( is_odd,
_1 ) ) { executed =
true; },
);
assert( executed );
}
Individual entries
Match for std::pair
int main()
{
using namespace std::string_literals;
{
(
Case( 10,
"H" ) {
return true; },
);
assert( result );
}
{
(
Case(
_2 ==
"Hello" ) {
return true; },
);
assert( !result );
}
{
(
Case( 10,
"Hello" ) {
return true; },
);
assert( !result );
}
}
Match for std::tuple
int main()
{
{
(
Case( 1, 0, 0, 1 ) {
return 9; },
Case( 1, 1, 1, 1 ) {
return 15; },
);
assert( result == 9 );
}
{
(
Case(
_2 == 0 &&
_3 == 0 ) {
return true; },
);
assert( result );
}
{
(
Case( 1, 1, 0, 1 ) {
return 13; },
Case( 1, 1, 1, 1 ) {
return 15; },
);
assert( result == -1 );
}
}
Predicates
Free function predicate
enum Place{ california, washington, new_york, new_jersey, las_vegas };
bool is_nonzero( const int i ) { return i != 0; }
bool is_positive( const int i, const int j ) { return i > 0 && j > 0; }
int main()
{
{
bool executed = false;
(
Case( ( is_nonzero,
_1 ) || ( is_nonzero,
_2 ) ) { executed =
true; },
);
assert( executed );
}
{
bool executed = false;
(
Case( ( is_positive,
_1,
_2 ) ) { executed =
true; },
);
assert( executed );
}
}
Lambda predicate
enum Place{ california, washington, new_york, new_jersey, las_vegas };
int main()
{
auto is_nonzero = []( const int i ) { return i != 0; };
{
bool executed = false;
(
Case( ( is_nonzero,
_1 ) ) { executed =
true; },
);
assert( executed );
}
{
bool executed = false;
(
Case( ( is_nonzero,
_1 ) || ( is_nonzero,
_2 ) ) { executed =
true; },
);
assert( executed );
}
auto is_positive = []( const int i, const int j ) { return i > 0 && j > 0; };
{
bool executed = false;
(
Case( ( is_positive,
_1,
_2 ) ) { executed =
true; },
);
assert( executed );
}
}
Regex
Just match
int main()
{
using namespace std::string_literals;
const auto phone_number = "^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\\s\\./0-9]*$"_r;
const auto email = "[\\w-]+@([\\w-]+\\.)+[\\w-]+"_r;
{
bool executed = false;
(
Case( phone_number ) { assert(
false ); },
Case( email ) { executed =
true; }
);
assert( executed );
}
{
bool executed = false;
(
Case( phone_number ) { executed =
true; },
Case( email ) { assert(
false ); }
);
assert( executed );
}
}
Match and withdraw
{
{
{ "HTTP/1.1 200 OK" },
{ "Content-Lenght: 88" },
{ "Content-Type: text/html" }
};
return v;
}
int main()
{
using namespace std::string_literals;
for( const auto & line : get_tokenized_http_response() )
{
const bool to_continue =
(
Case(
"^.+ 200 .+$"_r ) {
return true; },
fields[ match[ 1 ] ] = match[ 2 ];
return true;
},
);
if( !to_continue ) break;
}
assert( fields.
size() == 2 );
assert( fields[ "Content-Lenght"s ] == "88"s );
assert( fields[ "Content-Type"s ] == "text/html"s );
}
Withdrawing underlying value
Match for std::any
int main()
{
{
(
{
return f;
},
{
return i;
},
);
assert( result == 888 );
}
{
(
{
return f;
},
{
return i;
},
);
assert( result == -1 );
}
}
Match for std::variant<...>
int main()
{
(
{
},
{
return i;
},
);
assert( result == 5 );
}
Match for polymorphic types
struct base
{
virtual ~base() = default;
virtual int area() = 0;
};
struct circle : base{ int area() { return 10; } };
struct square : base{ int area() { return 20; } };
int main()
{
circle c;
base & b = c;
(
{
return c.area();
},
{
return s.area();
},
);
assert( result == 10 );
}
Falling properties
Explicit fallthrough
enum Place{ california, washington, new_york, new_jersey, las_vegas };
int main()
{
Place p = california;
{
bool executed_1st_case = false;
bool executed_2nd_case = false;
(
Case( new_york ) { executed_2nd_case =
true; },
);
assert( executed_1st_case && executed_2nd_case );
}
{
bool executed_1st_case = false;
bool executed_2nd_case = false;
bool executed_default_case = false;
(
Default { executed_default_case =
true; }
);
assert( executed_1st_case && executed_2nd_case && executed_default_case );
}
}
Implicit break
enum Place{ california, washington, new_york, new_jersey, las_vegas };
int main()
{
Place p = california;
(
Case(
_1 == california ) { assert(
false ); },
);
}
Utilities
any_from
enum Place{ california, washington, new_york, new_jersey, las_vegas };
int main()
{
for( const auto p : { california, washington, new_york } )
{
bool executed = false;
(
Case(
_1 ==
any_from( california, washington, new_york ) ) { executed =
true; },
);
assert( executed );
}
}
Stringification
Enum to string
enum Place{ california, washington, new_york, new_jersey, las_vegas };
{
(
Case( california ) {
return "california"; },
Case( washington ) {
return "washington"; },
Case( new_york ) {
return "new_york"; },
Case( new_jersey ) {
return "new_jersey"; },
Case( las_vegas ) {
return "las_vegas"; },
);
}
int main()
{
const std::string actualResult[] = {
"california",
"washington",
"new_york",
"new_jersey",
"las_vegas" };
for( const auto p : { california, washington, new_york, new_jersey, las_vegas } )
{
}
}
Customization
Floating point comparison
struct double_value
{
double value;
};
bool operator==(
const double d,
const double_value f )
{
return fabs( d - f.value ) < __FLT_EPSILON__;
}
double_value operator""_dbl( const long double f )
{
return { static_cast< double >( f ) };
}
int main()
{
(
);
}
Value and Type transferring
template< typename T >
struct Holder{};
struct Has_value{};
struct Has_type {};
template< eswitch_v5::NoneIndex T >
{
if constexpr( requires{ T_::value; } )
else
return false;
}
template< eswitch_v5::NoneIndex T >
{
if constexpr( requires{ typename T_::type; } )
else
return false;
}
int main()
{
(
Case( Has_value{} )(
int transferred_value )
{
return transferred_value;
},
Case( Has_type{} )( Holder< int > transferred_type )
{
return sizeof( int );
},
);
assert( r == sizeof( int ) );
}