#include <iostream>
#include <atomic>
template<typename T>
class foo_1 {
T state_;
public:
// variant 1
T const& getState() { return state_; }
void setState(T const& v) { state_ = v; }
// variant 2
T const& state() { return state_; }
void state(T const& v) { state_ = v; }
};
template<typename T>
class foo_3 {
T state_;
public:
// variant 3
T& state() { return state_; }
};
template<typename T>
class foo_4 {
public:
// variant 4
std::atomic<T> state;
};
using namespace std;
int main(int argc, char* argv[]) {
foo_1<int> bar1;
foo_3<int> bar3;
foo_4<int> bar4;
int rstate = 0;
bar1.setState(1);
rstate = bar1.getState();
cout << "variant 1:\tstate = " << rstate << endl;
bar1.state(2);
rstate = bar1.state();
cout << "variant 2:\tstate = " << rstate << endl;
bar3.state() = 3;
rstate = bar3.state();
cout << "variant 3:\tstate = " << rstate << endl;
bar4.state = 4;
rstate = bar4.state;
cout << "variant 4:\tstate = " << rstate << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8YXRvbWljPgoKdGVtcGxhdGU8dHlwZW5hbWUgVD4KY2xhc3MgZm9vXzEgewogIFQgc3RhdGVfOwpwdWJsaWM6CiAgLy8gdmFyaWFudCAxCiAgVCBjb25zdCYgZ2V0U3RhdGUoKSB7IHJldHVybiBzdGF0ZV87IH0KICB2b2lkIHNldFN0YXRlKFQgY29uc3QmIHYpIHsgc3RhdGVfID0gdjsgfQogIAogIC8vIHZhcmlhbnQgMgogIFQgY29uc3QmIHN0YXRlKCkgeyByZXR1cm4gc3RhdGVfOyB9CiAgdm9pZCBzdGF0ZShUIGNvbnN0JiB2KSB7IHN0YXRlXyA9IHY7IH0KfTsKCgp0ZW1wbGF0ZTx0eXBlbmFtZSBUPgpjbGFzcyBmb29fMyB7CiAgVCBzdGF0ZV87CnB1YmxpYzoKICAvLyB2YXJpYW50IDMKICBUJiBzdGF0ZSgpIHsgcmV0dXJuIHN0YXRlXzsgfQp9OwoKdGVtcGxhdGU8dHlwZW5hbWUgVD4KY2xhc3MgZm9vXzQgewpwdWJsaWM6Ci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