Created
August 22, 2017 05:33
-
-
Save mgarg1/a70d9babfe09c99fc9670e2ecc849d05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <iostream> | |
#define SWITCH(CASE, VALUE) case CASE : return VALUE; | |
#define LIST_OF_VARIABLES \ | |
SWITCH(2 , "two") \ | |
SWITCH(5 , "five") \ | |
SWITCH(7 , "seven") | |
template<class RetValue> | |
RetValue switchReturn(const int key){ | |
// makes the actual switch case | |
switch(key) { | |
LIST_OF_VARIABLES | |
default: throw "fail"; | |
} | |
#undef SWITCH | |
} | |
template<class RetValue> | |
void match(const int key,RetValue &val) { | |
val = switchReturn<RetValue>(key); | |
} | |
int main(){ | |
int num = 5; | |
std::string str; | |
match(num,str); | |
std::cout << str; | |
//here str == "five" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment