Created
April 12, 2016 16:07
-
-
Save amullins83/e5dc4e1cf14d1bb83d05cb64dae537b9 to your computer and use it in GitHub Desktop.
Generic operator<< definition?
This file contains hidden or 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 <iostream> | |
#include <string> | |
template <typename T> | |
std::ostream& streamOutEnum(std::ostream& os, T thing) | |
{ | |
return os << GetString(thing); | |
} | |
enum Type1 { | |
What, | |
Hey | |
}; | |
enum Type2 { | |
Yo, | |
Mama | |
}; | |
std::string GetString(Type1 thing) | |
{ | |
return thing == What ? "What" : "Hey"; | |
} | |
std::string GetString(Type2 thing) | |
{ | |
return thing == Yo ? "Yo" : "Mama"; | |
} | |
std::ostream& operator<<(std::ostream& os, Type1 thing) | |
{ | |
return streamOutEnum<Type1>(os, thing); | |
} | |
std::ostream& operator<<(std::ostream& os, Type2 thing) | |
{ | |
return streamOutEnum<Type2>(os, thing); | |
} | |
int main() | |
{ | |
Type1 thing = What; | |
Type2 other = Mama; | |
std::cout << thing << std::endl; | |
std::cout << other << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment