std::make_shared<>(), has access to protected constructor
Hello, I'm using Intel's 12.1 compiler with Visual Studio 2010 IDE, making 64 bit target. See code in 2 attached files.
The problem is that this code compiles and it should trigger an error. As expected, it fails with the Microsoft compiler with error C2248 cannot access protected member... .
The Worker class constructor is not public and so it should not be accessible from std::make_shared(...). Is this a compiler defect?
I have tried things such as making the Worker constructor private with the same outcome.
I tried completely removing the Worker constructor with 2 arguments, and the code still compiles with no complaint.
I tried making the call to std::make_shared in the constructor body rather than in member initialization. No improvement.
It seems like the compiler is not checking for access to construct the object managed by the std::shared_ptr<...>.
Thanks for looking
I can reproduce the problem you described. In my opinion this should not be the case -- any compiler has to error here. Hence I've filed a defect ticket (DPD200239413) and let you know about the status.
Thank you for the great reproducer!
>>...The Worker class constructor is not public and so it should not be accessible from std::make_shared(...). Is this a compiler defect?
Yes. Even if a member mpWorker is declared without the std::shared_ptr template class ( not as a smart pointer ):
Worker * operator ->() const;
// std::shared_ptr< Worker > mpWorker;
any C++compiler should fail with some error, like:
...'Worker::Worker' : cannot access protected member declared in class 'Worker'...
>>... I don't know the scope of the compiler issue...
I personally would use a workaround ( Is that a big problem? No. ) because it will take some time to fix the problem with the compiler. At least you should be able to proceed with what you need to do etc.