fork 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. int main() {
  20. Vector<float> float_vec;
  21. ComplexVector<float> complex_vec;
  22.  
  23. std::cout << "Is float_vec complex? " << float_vec.is_complex() << "\n";
  24. std::cout << "Is complex_vec complex? " << complex_vec.is_complex() << "\n";
  25.  
  26. // The following line doesn't compile
  27. // std::cout << "Is only_for_complex method in float_vec? " << float_vec.only_for_complex() << "\n";
  28. std::cout << "Is only_for_complex method in complex_vec? " << complex_vec.only_for_complex() << "\n";
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
Is float_vec complex? 0
Is complex_vec complex? 1
Is only_for_complex method in complex_vec? 1