概述
以下三个函数定义为头文件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++ Utilities四(Uninitialized memory的使用)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复