fork download
  1. #include <mutex>
  2. #include <map>
  3.  
  4. template <class T>
  5. struct Proxy{
  6. Proxy(T &t, std::mutex &m) : value{t}, l{m}{}
  7. T &value;
  8. private:
  9. std::unique_lock<std::mutex> l;
  10. };
  11.  
  12. template <class T>
  13. struct Protected{
  14. Proxy<T> get(){
  15. return {t, m};
  16. }
  17. private:
  18. T t;
  19. std::mutex m;
  20. };
  21.  
  22. int main() {
  23. Protected<std::map<int, int>> pm;
  24. pm.get().value[42] = 5;
  25. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Standard output is empty