Skip to content

Latest commit

 

History

History
63 lines (49 loc) · 1.16 KB

README.md

File metadata and controls

63 lines (49 loc) · 1.16 KB

is in

Ever wrote a lengthy if statement comparing a variable with some values?

if ((event == ERROR_1) ||
    (event == ERROR_2) ||
    (event == ERROR_3) ||
    ...
{
    // process
}

Ever missed the elegant Pythonic idiom in C++ code?

if event in [ERROR_1, ERROR_2, ERROR_3, ...]:
    # process

Miss no longer as this little library makes it possible to transform the first if statement to python-like below code:

if (event is_in({ERROR_1, ERROR_2, ERROR_3, ...}))  // notice just a whitespace separating event and is_in and the braces
{
    // process
}

Checking in containers is also supported:

std::vector<Events> evs = {ERROR_1, ERROR_2, ERROR_3, ...};
if (event is_in(evs))
{
    // process
}

Below is a short example. Compile it and try running with several command-line arguments.

#include "isin.h"
#include <iostream>

int main(int argc, char * /*argv*/[])
{
    if (argc is_in({2, 3, 4, 5}))
    {
        std::cout << "true\n";
    }
    else
    {
        std::cout << "false\n";
    }
}

License

This project is released under MIT/X11 license, so feel free to do anything you like with it.