fork download
  1. #include <iostream>
  2.  
  3. template <int limit> class NegativeNumber
  4. {
  5. public:
  6. NegativeNumber() : current(0) {};
  7.  
  8. int operator()()
  9. {
  10. return -(1 + (current++ % limit));
  11. };
  12. private:
  13. int current;
  14. };
  15.  
  16. class NegativeNumberNoTemplate
  17. {
  18. public:
  19. NegativeNumberNoTemplate(int limit) : m_limit(limit), current(0) {};
  20.  
  21. int operator()()
  22. {
  23. return -(1 + (current++ % m_limit));
  24. };
  25. private:
  26. const int m_limit;
  27. int current;
  28. };
  29.  
  30. void f5(NegativeNumber<5> &n)
  31. {
  32. std::cout << "limit five: " << n() << '\n';
  33. }
  34.  
  35. void f2(NegativeNumber<2> &n)
  36. {
  37. std::cout << "limit two: " << n() << '\n';
  38. }
  39.  
  40. void f(NegativeNumberNoTemplate &n)
  41. {
  42. std::cout << "no template: " << n() << '\n';
  43. }
  44.  
  45. int main(int argc, char **argv)
  46. {
  47. NegativeNumber<5> five;
  48. NegativeNumber<2> two;
  49. NegativeNumberNoTemplate notemplate(3);
  50.  
  51. for (int x = 0; x < 7; ++x)
  52. {
  53. std::cout << "limit five: " << five() << "\tlimit two: " << two() << '\n';
  54. std::cout << "no template: " << notemplate() << '\n';
  55. }
  56.  
  57. f5(five);
  58. f2(two);
  59. f(notemplate);
  60.  
  61. return 0;
  62. }
Success #stdin #stdout 0s 2884KB
stdin
Standard input is empty
stdout
limit five: -1	limit two: -1
no template: -1
limit five: -2	limit two: -2
no template: -2
limit five: -3	limit two: -1
no template: -3
limit five: -4	limit two: -2
no template: -1
limit five: -5	limit two: -1
no template: -2
limit five: -1	limit two: -2
no template: -3
limit five: -2	limit two: -1
no template: -1
limit five: -3
limit two: -2
no template: -2