fork download
  1. #include <iostream>
  2. #include <typeinfo>
  3. #include <cxxabi.h>
  4. #include <memory>
  5.  
  6. template <class T>
  7. std::string GetTypeName()
  8. {
  9. //http://stackoverflow.com/questions/23266391/find-out-the-type-of-auto/23266701#23266701
  10. std::unique_ptr<char, void(*)(void*)> name{abi::__cxa_demangle(typeid(T).name(), 0, 0, nullptr), std::free};
  11. return name.get();
  12. }
  13.  
  14.  
  15. struct A
  16. {
  17. struct AA {};
  18. struct AB {};
  19. struct AC {};
  20.  
  21. struct BA {};
  22. struct BB {};
  23. struct BC {};
  24.  
  25. BA GetBTypeFromAType(AA a) { return BA(); }
  26. BB GetBTypeFromAType(AB a) { return BB(); }
  27. BC GetBTypeFromAType(AC a) { return BC(); }
  28.  
  29. template <typename AType>
  30. void Func(AType a)
  31. {
  32. //using BType = typename std::result_of<decltype(GetBTypeFromAType(a))>::type;
  33. std::cout << "Func called with " << GetTypeName<AType>() << " and got " << GetTypeName<decltype(GetBTypeFromAType(a))>() << std::endl;
  34. }
  35. };
  36.  
  37. int main()
  38. {
  39. A a;
  40. A::AA aa;
  41. A::AB ab;
  42. A::AC ac;
  43. a.Func(aa);
  44. a.Func(ab);
  45. a.Func(ac);
  46. return 0;
  47. }
Success #stdin #stdout 0s 4516KB
stdin
Standard input is empty
stdout
Func called with A::AA and got A::BA
Func called with A::AB and got A::BB
Func called with A::AC and got A::BC