fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. enum Flavor { Cherry, Plum, Raspberry };
  5.  
  6. template <Flavor> using Number = double;
  7.  
  8. void printCherryNumber(Number<Cherry> num) { cout << num << endl; }
  9.  
  10. template <typename T> void printCherryNumber(T num) = delete;
  11.  
  12. // Doesn't work. "Cannot infer type argument F."
  13. template <Flavor F>
  14. void printPlumNumber(Number<F> num) {
  15. static_assert(F == Plum, "Wrong number flavor!");
  16. cout << num << endl;
  17. }
  18.  
  19. // Doesn't work. Compiler rightly treats the call as ambiguous.
  20. template <Flavor F> void printRaspberryNumber(Number<F> num) = delete;
  21.  
  22. template <> void printRaspberryNumber<Raspberry>(Number<Raspberry> num) {
  23. cout << num << endl;
  24. }
  25.  
  26. int main() {
  27. Number<Cherry> a(5);
  28. Number<Plum> b(6);
  29. Number<Raspberry> c(3.1415);
  30.  
  31. printCherryNumber(a);
  32. printCherryNumber(b); // O, if only this could be a compiler error.
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
5
6