fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct A
  5. {
  6. unsigned int value = 0;
  7. };
  8.  
  9. A get_set(const A* user_a = nullptr)
  10. {
  11. static A a = { 31 };
  12. // This need to be thread safe
  13. // Multiple readers, one writer
  14. if(user_a)
  15. {
  16. // Copy
  17. a = *user_a;
  18. }
  19. return a;
  20. }
  21.  
  22. A default_a() {
  23. return get_set();
  24. }
  25.  
  26.  
  27. A switch_a(const A& user_a) {
  28. A old_a = get_set();
  29. get_set(&user_a);
  30. return old_a;
  31. }
  32.  
  33. void set_a(const A& user_a) {
  34. get_set(&user_a);
  35. }
  36.  
  37.  
  38. int main() {
  39. {
  40. A def_a = default_a();
  41. cout << def_a.value << endl;
  42. }
  43.  
  44. {
  45. A my_a = { 32 };
  46. A old_a = switch_a(my_a);
  47. cout << old_a.value << endl;
  48. A def_a = default_a();
  49. cout << def_a.value << endl;
  50. }
  51.  
  52. {
  53. A my_a = { 33 };
  54. set_a(my_a);
  55. A def_a = default_a();
  56. cout << def_a.value << endl;
  57. }
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
31
31
32
33