I try to create a singleton object using shared_ptrs. However the code does not compile when the constructors/destructor are private for the specific object The code is below.h
//ThreadPool.h
class ThreadPool
{
public:
static std::shared_ptr<ThreadPool> & getInstance();
inline static std::shared_ptr<ThreadPool> m_threadPoolInstance;
private:
ThreadPool() =default;
~ ThreadPool() = default;
ThreadPool(ThreadPool const &) = default;
};
//ThreadPool.cpp
#include "pch.h"
#include <ThreadPool.h>
std::shared_ptr<ThreadPool> & ThreadPool::getInstance()
{
if (! m_threadPoolInstance)
{
ThreadPool * p_ThreadPool = new ThreadPool();
m_threadPoolInstance.reset(p_ThreadPool);
}
return m_threadPoolInstance;
}
I am using VS17 compiler The error that is created is the following
error C2440: '': cannot convert from '_Ux *' to 'std::shared_ptr' with [ _Ux=ThreadPool ] include\memory(1462): note: No constructor could take the source type, or constructor overload resolution was ambiguous threadpool.cpp(9): note: see reference to function template instantiation 'void std::shared_ptr::reset(_Ux *)' being compiled with [ _Ux=ThreadPool ] threadpool.cpp(9): note: see reference to function template instantiation 'void std::shared_ptr::reset(_Ux *)' being compiled with [ _Ux=ThreadPool ]
When I set the constructors/destructor in public section, the compilation is succesfull.
However running the same code using gcc compiler , compiles succesfully