I wanted to write a simple container / factory and prohibit the creation of an object in other ways, but I ran into an unexpected problem. Simplified code:
struct MyObject { friend struct MyObjectContainer; private: MyObject(int param) { _param = param; } int _param; }; struct MyObjectContainer : private std::vector<MyObject> { void Add(int param) { emplace_back(param); } } objs; The compiler gives an error:
c: \ program files (x86) \ microsoft visual studio 14.0 \ vc \ include \ xmemory0 (637): error C2248: 'MyObject :: MyObject': 'MyObject'
When replacing emplace_back (param) with push_back (MyObject (param)), the code works, but I would like to find a way to use a more rational method of emplace_back.