fork(3) download
  1. #include <iostream>
  2. #include <type_traits>
  3.  
  4. // Traits class
  5. template <typename T, typename = void>
  6. struct VectorTraits;
  7.  
  8. template <typename T>
  9. struct VectorTraits<T, std::enable_if_t<(T::dimension > 0)>>
  10. {
  11. typedef T VectorType;
  12. typedef typename T::ValueType ValueType;
  13. static const std::uint16_t dimension = T::dimension;
  14. };
  15.  
  16.  
  17. // Fake vector class. Defines the required typedef.
  18. struct Vec
  19. {
  20. typedef float ValueType;
  21. static const std::uint16_t dimension = 2;
  22. };
  23.  
  24. // Fake vector class. Defines the required typedef.
  25. struct VecFake
  26. {
  27. };
  28.  
  29. template <>
  30. struct VectorTraits<VecFake>
  31. {
  32. typedef VecFake VectorType;
  33. typedef float ValueType;
  34. static const std::uint16_t dimension = 12;
  35. };
  36.  
  37. // Streaming operator attempt for classes defining VectorTraits.
  38. template <class T, typename = std::enable_if_t<(sizeof(VectorTraits<T>) > 0)>>
  39. std::ostream& operator << (std::ostream& stream, const T& )
  40. {
  41. return stream << "Traits. Dimension = " << VectorTraits<T>::dimension << "\n";
  42. }
  43.  
  44. int main()
  45. {
  46. std::cout << "Test\n";
  47. std::cout << Vec();
  48. std::cout << VecFake();
  49. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Test
Traits. Dimension = 2
Traits. Dimension = 12