fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class A0{ //saparate here to make identity
  5. public:
  6. enum ENU{
  7. T1,T2
  8. };
  9. };
  10. namespace namesp {
  11. template<typename T>
  12. struct identity { typedef T type; };
  13.  
  14. template <A0::ENU T>
  15. struct EnumToBuiltin {
  16. };
  17.  
  18. template <>
  19. struct EnumToBuiltin<A0::T1> {
  20. typedef int type;
  21. };
  22. template <>
  23. struct EnumToBuiltin<A0::T2> {
  24. typedef std::string type;
  25. };
  26. }
  27.  
  28. class B : public A0{
  29. public:
  30. int b=123;
  31. std::string c="I am C";
  32. template<ENU ttt> typename namesp::EnumToBuiltin<ttt>::type f(){
  33. return getField_(namesp::EnumToBuiltin<ttt>());
  34. }
  35. int getField_(namesp::EnumToBuiltin<T1>){
  36. return b;
  37. }
  38. std::string getField_(namesp::EnumToBuiltin<T2>){
  39. return c;
  40. }
  41. };
  42.  
  43. int main() {
  44. B b;
  45. cout<< b.f<B::T1>(); //int
  46. cout<< b.f<B::T2>(); //string
  47. return 0;
  48. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
123I am C