Skip to content

Instantly share code, notes, and snippets.

@amullins83
Created April 12, 2016 16:07
Show Gist options
  • Save amullins83/e5dc4e1cf14d1bb83d05cb64dae537b9 to your computer and use it in GitHub Desktop.
Save amullins83/e5dc4e1cf14d1bb83d05cb64dae537b9 to your computer and use it in GitHub Desktop.
Generic operator<< definition?
#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