fork download
  1. #include <iostream>
  2.  
  3. template<int src, int dst>
  4. struct move_disc
  5. {
  6. move_disc() {
  7. std::cout << "move " << src << " to " << dst << "\n";
  8. }
  9.  
  10. };
  11.  
  12. template<int n, int src, int tmp, int dst>
  13. struct hanoi
  14. {
  15. hanoi<n-1, src, dst, tmp> before;
  16. move_disc<src, dst> disc; // normal member
  17. hanoi<n-1, tmp, src, dst> after;
  18. };
  19.  
  20. template<int src, int tmp, int dst>
  21. struct hanoi<0, src, tmp, dst>
  22. {
  23. // recursive base case
  24. };
  25.  
  26.  
  27. int main() {
  28. hanoi<3, 1, 2, 3> go;
  29. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
move 1 to 3
move 1 to 2
move 3 to 2
move 1 to 3
move 2 to 1
move 2 to 3
move 1 to 3