fork download
  1. #include <iostream>
  2. #include <atomic>
  3.  
  4. template<typename T>
  5. class foo_1 {
  6. T state_;
  7. public:
  8. // variant 1
  9. T const& getState() { return state_; }
  10. void setState(T const& v) { state_ = v; }
  11.  
  12. // variant 2
  13. T const& state() { return state_; }
  14. void state(T const& v) { state_ = v; }
  15. };
  16.  
  17.  
  18. template<typename T>
  19. class foo_3 {
  20. T state_;
  21. public:
  22. // variant 3
  23. T& state() { return state_; }
  24. };
  25.  
  26. template<typename T>
  27. class foo_4 {
  28. public:
  29. // variant 4
  30. std::atomic<T> state;
  31. };
  32.  
  33.  
  34. using namespace std;
  35.  
  36. int main(int argc, char* argv[]) {
  37. foo_1<int> bar1;
  38. foo_3<int> bar3;
  39. foo_4<int> bar4;
  40.  
  41. int rstate = 0;
  42.  
  43. bar1.setState(1);
  44. rstate = bar1.getState();
  45. cout << "variant 1:\tstate = " << rstate << endl;
  46.  
  47. bar1.state(2);
  48. rstate = bar1.state();
  49. cout << "variant 2:\tstate = " << rstate << endl;
  50.  
  51. bar3.state() = 3;
  52. rstate = bar3.state();
  53. cout << "variant 3:\tstate = " << rstate << endl;
  54.  
  55. bar4.state = 4;
  56. rstate = bar4.state;
  57. cout << "variant 4:\tstate = " << rstate << endl;
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 2852KB
stdin
Standard input is empty
stdout
variant 1:	state = 1
variant 2:	state = 2
variant 3:	state = 3
variant 4:	state = 4