c++ - 让 Howard Hinnant 的 short_alloc(C++11 版本)在 Visual C++ 2015 中编译

标签 c++ visual-c++ c++11 memory allocation

我希望能够将自定义分配器与 std::vector 一起使用,以便将小数据缓冲区(例如,小于 1024 字节)存储在堆栈上,并且只有较长的 vector 存储在堆上。作为一个有 Fortran 背景的人,每次我必须进行堆内存分配以在五行子程序的持续时间内存储六个元素时,这让我感到 body 疼痛!

Howard Hinnant 发表了他的 short_alloc分配器完全符合我的要求,如果我用 gcc 编译它,它会很好用。但是,在 Visual C++ 中我无法编译它。在 Visual C++ 2013 中,部分问题是不受支持的 C++11 关键字太多,但即使我已将所有这些都#DEFINE 掉,我仍然遇到了问题。今天尝试用Visual C++ 2015 CTP 5编译,现在关键字都支持了,但同样的原因最终还是编译失败。

问题是这样的:出于我无法完全理解的原因,Hinnant 的代码默认使用复制构造函数但删除了复制赋值运算符:

short_alloc(const short_alloc&) = default;
short_alloc& operator=(const short_alloc&) = delete;

尝试编译时,这会在 Visual C++ 中触发以下错误:

xmemory0(892): error C2280: 'short_alloc<int,1024> &short_alloc<1024>::operator =(const short_alloc<1024> &)': attempting to reference a deleted function

更让我困惑的是,如果我修改 Hinnant 的代码说

short_alloc(const short_alloc&) = default;
short_alloc& operator=(const short_alloc&) = default;

...然后我仍然得到完全相同的错误消息。

作为引用,这是我的测试代码:

#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;
}

如果我注释掉这行,编译错误就会消失

theVector = std::move(anothertestvec);

很明显,潜在的问题是 Visual C++ 处理复制的方式与 gcc 不同。即便如此,我还是不知道如何从这里开始。有没有办法让它在 Visual C++ 中工作?

最佳答案

我能想到的最简单的技巧就是替换

short_alloc& operator=(const short_alloc&) = delete;

short_alloc& operator=(const short_alloc&) 
{ 
    assert(false && "this should never be called"); 
    return *this; 
};

这看起来像是一个非常危险的黑客攻击,但实际上在这种特殊情况下并没有那么糟糕,原因如下:

原始版本不能在 VC++ 中编译的原因是它的标准库实现 std::vector的移动赋值运算符犯了测试的经典错误 std::allocator_traits<...>::propagate_on_container_move_assignment::value使用 if()声明。

如果特征值为 false,它会进行适当的检查并且不会分配分配器(如果分配器不同,则按照标准将元素单独移动到另一侧),但是 if() 上的代码分支仍然需要编译,即使对于这种类型的分配器永远不会达到它。

因此,该赋值运算符永远不会在运行时被容器的实现调用,这就是为什么在这种特殊情况下黑客攻击是安全的。

(最有趣的是,在 if() 下面的一行,实际的移动是通过使用标签调度的辅助函数正确实现的...)


这基于 Visual C++ 2013 Update 4 附带的标准库实现。

更新:正如OP在评论中所报告的,VC14 CTP5也有同样的问题。


更新 2:如 bug report 的评论中所示,此问题的修复程序将在 Visual C++ 2015 的最终版本中提供。

关于c++ - 让 Howard Hinnant 的 short_alloc(C++11 版本)在 Visual C++ 2015 中编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28296897/

相关文章:

c++11 - std::this_thread::sleep_for() 和纳秒

c++ - 运算符++的返回值

c++ - 更新模型/ View 场景中 QXXXView 元素(QTableView、QTreeView 等)的显示工具提示

c++ - Base 64 编码丢失数据

c++ - 为什么 std::atomic 在 Visual C++ 中不是简单的类型?

C++:在初始化程序中类静态变量的定义中查找名称

c++ - 记录dll API然后模仿它

c++ - 删除类的复制构造函数时初始化类的数组成员

visual-c++ - 将 VC++ 程序集部署到 Azure

c++ - 为什么 <const char*> 和 <const char[]> 有非常不同的内存或指针行为?