Man*_*uel 2 c++ initialization fill
是否可以使用std::fill初始化非POD类型的数组?
该文件说,std::fill使用operator=初始化数组没有放置副本建设.但是,就我所见,赋值运算符在未初始化空间上调用它时,实际上没有机会释放任何当前内存.
例:
struct NonPod
{
std::string myStr;
};
NonPod arr[10];
NonPod prototype;
NonPod * ptr = &arr[0];
std::fill_n(ptr, 10, prototype);
Run Code Online (Sandbox Code Playgroud)
您正在寻找std::uninitialized_fill_n从memory头,不std::fill_n从algorithm头.
但要注意!您的代码不考虑对齐或填充 - 考虑std::alignment_of在不可用的平台上使用或适当的boost替换.