Skip to content

chardan/vswitch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

vswitch

Function-like value switch.

...easily create switch-like constructs with your choice of type.

enum struct colors { red, blue, yellow, unknown };

 colors color = vswitch<colors, string>(argv[1])
    .result(colors::red, "red", "crimson", "vermillion")
    .result(colors::blue, "blue", "azure", "cerulian")
    .result(colors::yellow, "yellow")
    .defaultas(colors::unknown);

 switch(color) { ... }

...the result of the switch is (a copy of) the selected value, so you can say:

 colors color = vswitch<string, string(argv[1])
	.result("red", "red", "crimson", "scarlet")
    .nodefault();

 cout << "matched: " << color << '\n';

Using nodefault() will cause the library to throw a std::range_error if no value has matched, or return the result of the match (in this case, "red").

About

Function-like value switch.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published