• How about this one:

    template<class T>
    T* SingletonInstance(std::function<T*()> init_func = []{return new T(); }) {
    	static T* _instance = nullptr;
    	if (!_instance) {
    		if (init_func) {
    			_instance = init_func();
    	return _instance;
    #define SINGLETON_INSTANCE(cls, ...) SingletonInstance<cls>([]{return new cls(__VA_ARGS__); });
    //auto service = SINGLETON_INSTANCE(AServiceClass);
    //auto service = SINGLETON_INSTANCE(AServiceClass, 2019, 996);

    Might save that "one line"?

    Edited by EthanNote
  • both of them are not perfect. Because client still is able to create instance with default constructors.

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment