fork(1) download
  1. #include <iostream>
  2. #include <complex>
  3.  
  4. template <typename T>
  5. class Vector
  6. {
  7. public:
  8. bool is_complex() { return false; }
  9. };
  10.  
  11. template <typename U>
  12. class ComplexVector : Vector<std::complex<U>>
  13. {
  14. public:
  15. bool is_complex() { return true; }
  16. bool only_for_complex() { return true; }
  17. };
  18.  
  19. // For any type T, value is false.
  20. template <typename T>
  21. struct is_complex_vector
  22. {
  23. static const bool value = false;
  24. };
  25.  
  26. // We specialize the struct so that, for any type U,
  27. // passing ComplexVector<U> makes value true
  28. template <>
  29. template <typename U>
  30. struct is_complex_vector<ComplexVector<U>>
  31. {
  32. static const bool value = true;
  33. };
  34.  
  35. int main() {
  36. Vector<float> float_vec;
  37. ComplexVector<float> complex_vec;
  38.  
  39. std::cout << "Is float_vec a ComplexVector type? " << is_complex_vector<typeof(float_vec)>::value << "\n";
  40. std::cout << "Is complex_vec a ComplexVector type? " << is_complex_vector<typeof(complex_vec)>::value << "\n";
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Is float_vec a ComplexVector type? 0
Is complex_vec a ComplexVector type? 1