Skip to content

Instantly share code, notes, and snippets.

@mgarg1
Created August 22, 2017 05:33
Show Gist options
  • Save mgarg1/a70d9babfe09c99fc9670e2ecc849d05 to your computer and use it in GitHub Desktop.
Save mgarg1/a70d9babfe09c99fc9670e2ecc849d05 to your computer and use it in GitHub Desktop.
#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