我是靠谱客的博主 发嗲发卡,这篇文章主要介绍C++ Utilities四(Uninitialized memory的使用),现在分享给大家,希望可以做个参考。

以下三个函数定义为头文件memory中,下面是其可能的实际代码。代码来源于《The C++ Standard Library》
namespace std
{
	template<class ForwIter,class T>
	void uninitialized_fill(ForwIter beg,ForwIter end, const T& value)
	{
		typedef typename iterator_traits<ForwIter>::value_type VT;
		ForwIter save(beg);
		try{
			for(;beg != end; ++beg)
			{
				new (static_cast<void *>(& *beg)) VT(value);
			}
		}catch(...)
		{
			for(; save != beg; ++save)
			{
				save->~VT();
			}
			throw;
		}
	}
	
	template<class ForwIter,class Size,class T>
	void uninitialized_fill_n(ForwIter beg,Size num, const T& value)
	{
		typedef typename iterator_traits<ForwIter>::value_type VT;
		ForwIter save(beg);
		try{
			for(;num--; ++beg)
			{
				new (static_cast<void *>(& *beg)) VT(value);
			}
		}catch(...)
		{
			for(; save != beg; ++save)
			{
				save->~VT();
			}
			throw;
		}
	}
	
	template<class InputIter,class ForwIter>
	void uninitialized_copy(InputIter beg,InputIter end, ForwIter dest)
	{
		typedef typename iterator_traits<ForwIter>::value_type VT;
		ForwIter save(dest);
		try{
			for(;beg != end; ++beg,++dest)
			{
				new (static_cast<void *>(& *dest)) VT(*beg);
			}
		}catch(...)
		{
			for(; save != dest; ++save)
			{
				save->~VT();
			}
			throw;
		}
	}
}


最后

以上就是发嗲发卡最近收集整理的关于C++ Utilities四(Uninitialized memory的使用)的全部内容,更多相关C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部