Eos*_*ern 6 c++ memory allocation visual-c++ c++11
我希望能够使用带有std :: vector的自定义分配器,以便在堆栈上存储小数据缓冲区(例如,小于1024字节),并且只有更长的向量存储在堆上.作为拥有Fortran背景的人,每次我必须在五行子程序的持续时间内进行堆内存分配以存储六个元素时,它会让我感到身体疼痛!
Howard Hinnant发布了他的short_alloc分配器,它正是我正在寻找的东西,如果我用gcc编译它就可以了.但是,在Visual C++中,我无法编译它.在Visual C++ 2013中,部分问题是太多的C++ 11关键字不受支持,但即使我已经#DEFINE'd所有这些关键字,我仍然遇到了问题.今天我尝试在Visual C++ 2015 CTP 5中进行编译,现在所有关键字都得到了支持,但编译最终因同样的原因而失败.
问题是:由于一个我不能声称完全理解的原因,Hinnant的代码默认复制构造函数但删除了复制赋值运算符:
short_alloc(const short_alloc&) = default;
short_alloc& operator=(const short_alloc&) = delete;
Run Code Online (Sandbox Code Playgroud)
尝试编译时,这会在Visual C++中触发以下错误:
xmemory0(892): error C2280: 'short_alloc<int,1024> &short_alloc<1024>::operator =(const short_alloc<1024> &)': attempting to reference a deleted function
Run Code Online (Sandbox Code Playgroud)
令我更加困惑的是,如果我修改Hinnant的代码来说
short_alloc(const short_alloc&) = default;
short_alloc& operator=(const short_alloc&) = default;
Run Code Online (Sandbox Code Playgroud)
...然后我仍然获得完全相同的错误消息.
作为参考,这是我的测试代码:
#include <iostream>
#include <vector>
#include "short_alloc.h"
void populate_the_vector(std::vector<int, short_alloc<int, 1024> > &theVector)
{
arena<1024> B;
std::vector<int, short_alloc<int, 1024> > anothertestvec{(short_alloc<int, 1024>(B))};
anothertestvec.resize(10);
for (int i=0; i<10; ++i)
{
anothertestvec[i] = i;
}
theVector = std::move(anothertestvec); // Actually causes a copy, as the Arenas are different
}
int main()
{
arena<1024> A;
std::vector<int, short_alloc<int, 1024> > testvec{(short_alloc<int, 1024>(A))};
populate_the_vector(testvec);
printf("Testvec(10)=%d\r\n", testvec[5]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果我注释掉这行说法,编译错误就会消失
theVector = std::move(anothertestvec);
Run Code Online (Sandbox Code Playgroud)
显然,潜在的问题是Visual C++以与gcc不同的方式处理副本.即便如此,我也不知道如何从这里开始.有没有办法让它在Visual C++中工作?
我能想到的最简单的黑客正在取代
short_alloc& operator=(const short_alloc&) = delete;
Run Code Online (Sandbox Code Playgroud)
同
short_alloc& operator=(const short_alloc&)
{
assert(false && "this should never be called");
return *this;
};
Run Code Online (Sandbox Code Playgroud)
它看起来像一个非常危险的黑客,但在这种特殊情况下它实际上并没有那么糟糕,这就是为什么:
原始版本不能在VC++中编译的原因是它std::vector的移动赋值运算符的标准库实现使得std::allocator_traits<...>::propagate_on_container_move_assignment::value使用if()语句进行测试的经典错误.
它进行了正确的检查,如果特征值是,则不分配分配器false(如果分配器不同,如标准所要求的那样,将元素单独移动到另一侧),但if()分支上的代码仍然需要编译,即使它永远不会达到这种类型的分配器.
因此,在运行时,容器的实现永远不会调用该赋值运算符,这就是为什么在这种特殊情况下hack是安全的.
(最有趣的是,在下面的一行,if()使用标签调度的辅助函数正确实现实际移动......)
这基于Visual C++ 2013 Update 4附带的标准库实现.
更新:正如OP在评论中报告的那样,VC14 CTP5也存在同样的问题.
更新2:如错误报告的注释中所示,此问题的修复程序将在Visual C++ 2015的最终版本中提供.