fork(1) download
  1. template<class T, class R = void>
  2. struct enable_if_type
  3. {
  4. typedef R type;
  5. };
  6.  
  7. template<class E, class Enable = void>
  8. struct GetFloatType
  9. {
  10. typedef E type;
  11. };
  12.  
  13. template<class E>
  14. struct GetFloatType<E, typename enable_if_type<typename E::Scalar>::type>
  15. {
  16. typedef typename E::Scalar type;
  17. };
  18.  
  19. template<class E>
  20. struct GetFloatType<E, typename enable_if_type<typename E::Inner>::type>
  21. {
  22. typedef typename E::Inner type;
  23. };
  24.  
  25. #include <typeinfo>
  26. #include <iostream>
  27. template <class Elemtype, class Floattype = typename GetFloatType<Elemtype>::type>
  28. class ExponentialSmoother
  29. {
  30. public:
  31. void print()
  32. {
  33. std::cout << "Elem: " << typeid(Elemtype).name()
  34. << ", Float: " << typeid(Floattype).name() << "\n";
  35. }
  36. };
  37.  
  38. template<typename T>
  39. class Vector
  40. {
  41. public:
  42. typedef T Scalar;
  43. };
  44.  
  45. template<typename T>
  46. class Vector2
  47. {
  48. public:
  49. typedef T Inner;
  50. };
  51.  
  52. class Vec : public Vector<double>, public Vector2<float>
  53. {
  54.  
  55. };
  56.  
  57. int main()
  58. {
  59. ExponentialSmoother<Vector<float> >().print();
  60. ExponentialSmoother<Vector2<float> >().print();
  61. ExponentialSmoother<double>().print();
  62. ExponentialSmoother<double,char>().print();
  63. // The following is ambiguous: Vec::Inner or Vec::Scalar ?
  64. //ExponentialSmoother<Vec>().print();
  65. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
Elem: 6VectorIfE, Float: f
Elem: 7Vector2IfE, Float: f
Elem: d, Float: d
Elem: d, Float: c