确认与MSVC 2015 RC中的分配器相关的此标准库错误

时间:2015-07-19 05:50:27

标签: c++ visual-c++ bug-reporting

这是SSCCE

#include <memory>
#include <vector>

template <class T> struct my_allocator : std::allocator<T> {
    //This overriding struct causes the error
    template <class U> struct rebind {
        typedef my_allocator<T> other;
    };

    //Ignore all this.
    typedef std::allocator<T> base;
    typename base::pointer allocate(typename base::size_type n, std::allocator<void>::const_pointer /*hint*/=nullptr) { return (T*)malloc(sizeof(T)*n); }
    void deallocate(typename base::pointer p, typename base::size_type /*n*/) { free(p); }
};

int main(int /*argc*/, char* /*argv*/[]) {
    std::vector<int,my_allocator<int>> vec;
    return 0;
}
海湾合作委员会喜欢它 ICC喜欢它。
Clang喜欢它。
甚至MSVC 2013也喜欢它 但MSVC 2015 RC吐出:

1>------ Build started: Project: Test Alloc, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(579): error C2664: 'void std::_Wrap_alloc<my_allocator<int>>::deallocate(int *,unsigned int)': cannot convert argument 1 from 'std::_Container_proxy *' to 'int *'
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(579): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(574): note: while compiling class template member function 'void std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>::_Free_proxy(void)'
1>          with
1>          [
1>              _Ty=int,
1>              _Alloc=my_allocator<int>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(541): note: see reference to function template instantiation 'void std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>::_Free_proxy(void)' being compiled
1>          with
1>          [
1>              _Ty=int,
1>              _Alloc=my_allocator<int>
1>          ]
1>  c:\program files (x86)\microsoft visual studio 14.0\vc\include\vector(668): note: see reference to class template instantiation 'std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>' being compiled
1>          with
1>          [
1>              _Ty=int,
1>              _Alloc=my_allocator<int>
1>          ]
1>  c:\users\ian mallett\desktop\test-alloc\main.cpp(18): note: see reference to class template instantiation 'std::vector<int,my_allocator<int>>' being compiled

相关程序给出类似的腥探测错误。这是两个:
错误C2664:'void std :: _ Wrap_alloc&gt; :: deallocate(int *,unsigned int)':无法将参数1从'std :: _ Container_proxy *'转换为'int *'
无法从'std :: _ Wrap_alloc&gt;'转换参数1到'const aligned_allocator&amp;'

布尔问题:这是一个错误吗? Iff是的,我会(try)提交它。

[编辑:如评论中所述,此仅在调试模式下发生。在发布模式下,它编译并执行正常。] [编辑:更简单的例子]

1 个答案:

答案 0 :(得分:4)

  

布尔问题:这是一个错误吗?

false

虽然MSVC给出的模板错误在这里是无用的,但这里的错误是我的(因为这个版本的标准库今天发货,所以可以放心)。

我从各种来源创建了这个分配器(以及后来的简化测试用例),这就是为什么我认为它是正确的。但是,正如评论中所建议的那样,我再次检查,这次是对文档的详尽反对。

此处缺少的组件是其中一个复制构造函数(无法自动生成的模板)。这仅在定义rebind结构时显示,因为rebind结构覆盖了父类中的相同结构(因为它在父类中,最终导致父类复制构造函数被调用,所以没有问题(除了它在技术上是错误的))。

这里有趣的是,错误直到现在才发生。正如我所说,GCC,Clang和MSVC 2013都喜欢它(即使它们各自的调试模式)。只是没有这些发生来调用模板复制构造函数。然而,标准指定了 ,因此错误最终是我的。

祝贺MSVC编译团队,并为噪音感到抱歉! :d