c++11总结07——改进单例模式

泛型单例存在的问题:

泛型单例要能够创建所有的类型对象,但是这些类型的构造函数形参不尽相同,参数个数和参数类型可能都不相同,导致我们不容易做一个所有类型都通用的单例。

c++11借助可变参数模板实现单例(上代码)

#include <iostream>
using namespace std;

template <typename T>
class Singleton
{
public:
	template<typename... Args>    //可变参数模板
	static T* Instance(Args&&... args)
	{
		if (m_pInstance == nullptr)
			m_pInstance = new T(std::forward<Args>(args)...);  //完美转发

		return m_pInstance;
	}


	static T* GetInstance()
	{
		if (m_pInstance == nullptr)
			throw std::logic_error("Init error");

		return m_pInstance;
	}

	//销毁
	static void DestroyInstance()
	{
		delete m_pInstance;
		m_pInstance = nullptr;
	}

private:
	Singleton(void);
	virtual ~Singleton(void);
	Singleton(const Singleton&);
	Singleton& oprator = (const Singleton&);  //拷贝构造函数

private:
	static T* m_pInstance;
};

template <class T> T* Singleton<T>::m_pInstance = nullptr;

struct func1
{
	func1(const string&)
	{
		cout << "l" << endl;
	}

	func1(string&& x)
	{
		cout << "r" << endl;
	}
};

struct func2
{
	func2(const string&)
	{
		cout << "l" << endl;
	}

	func2(string&& x)
	{
		cout << "r" << endl;
	}
};

struct func3
{
	func3(int m, float x) {}

	void fun()
	{
		cout << "func3" << endl;
	}
};

int main()
{
	string str = "test";

	Singleton<func1>::Instance(str);

	//move之后str变为右值,避免复制(右值引用的移动语义提高性能)
	Singleton<func2>::Instance(std::move(str));

	Singleton<func3>::Instance(1, 1.2);
	Singleton<func3>::Instance()->fun();

	//释放单例
	Singleton<func1>::DestroyInstance();
	Singleton<func2>::DestroyInstance();
	Singleton<func3>::DestroyInstance();

	system("pause");
    return 0;
}

c++11的可变参数模板消除了重复的可变参数的定义,避免重复的代码累加,同时支持完美转发,既避免了不必要的内存复制,无论是左值还是右值都能转发到正确的构造函数中,又增加了灵活性。

 

补充:

完美转发: 在函数模板中,完全依照模板的参数的类型(即保持函数的左值、右值特征),将参数传递给函数模板中调用的另一个函数。c++11中提供了std::forward实现完美转发。

右值引用、完美转发结合可变参数模板,可以实现一个万能的函数包装器

template <class Function, class... Args>

inline auto FuncWrapper(Function && f, Args && ... args) -> decltype(f(std::forward<Args>...))

{

           return f(std::forward<Args>(args)...);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值