确认MSVC 2015 RC中有关分配器的标准库Bug
Confirm This Standard Library Bug Relating to Allocators in MSVC 2015 RC
这是一个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;
}
GCC喜欢它。
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.0vcincludevector(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.0vcincludevector(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.0vcincludevector(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.0vcincludevector(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.0vcincludevector(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:usersian mallettdesktoptest-allocmain.cpp(18): note: see reference to class template instantiation 'std::vector<int,my_allocator<int>>' being compiled
相关程序给出类似的可疑错误。这里有两个:
错误C2664: 'void std::_Wrap_alloc>::deallocate(int *,unsigned int)':无法将参数1从'std::_Container_proxy *'转换为'int *'
无法将参数1从'std::_Wrap_alloc>'转换为'const aligned_allocator &'
布尔问题:这是一个bug吗?如果是的话,我会(尽量)提交的。
[EDIT:如注释所述,此仅在调试模式下出现。在发布模式下,它可以很好地编译和执行。[编辑:更简单的例子]
布尔问题:这是一个bug吗?
false
.
虽然MSVC在这里给出的模板错误非常没有帮助,但这里的错误是我的(放心,因为这个版本的标准库今天发布)。
我从各种来源创建了这个分配器(以及后来减少的测试用例),这就是为什么我认为它是正确的。然而,正如评论中建议的那样,我又检查了一遍,这次是详尽地对照文档。
这里缺少的组件是复制构造函数之一(不能自动生成的模板构造函数)。这只有在定义rebind
结构时才会出现,因为rebind
结构在父类中覆盖了相同的结构(由于它在父类中,最终导致调用父类的复制构造函数,所以没有问题(除了它在技术上是错误的))。
这里有趣的是错误直到现在才发生。正如我所说,GCC、Clang和MSVC 2013都喜欢它(即使使用各自的调试模式)。只是这些都没有发生调用模板复制构造函数。然而,如果是标准指定的,那么,错误最终是我的。
祝贺MSVC编译器团队,并为噪音感到抱歉!: D
- 弃用标准::分配器<void>
- 从标准容器重新绑定和复制分配器
- 如何在自定义容器中正确使用标准::分配器?
- GCC 如何实现 C++ 标准分配器
- 分配但不使用分配器的标准图书馆设施
- 在模板类中使用标准分配器时,模板声明的样子是什么样的
- 复制有状态分配器:标准库分配器语义和内部内存
- 更换标准C++分配器?
- 标准::映射的自定义分配器失败
- forward_list,设置,列表等如何调用标准::分配器
- 标准::矢量中的自定义分配器
- 符合 STL 标准的内存分配器库
- 用户定义分配器的非标准库放置新功能
- std::映射标准分配器性能与块分配器
- 如果标准容器元素类型和std::分配器类型不同,这是错误的吗
- 乘以定义的标准分配器
- 为什么在c++ 11中交换标准库容器会有问题(涉及分配器)
- 确认MSVC 2015 RC中有关分配器的标准库Bug
- 标准::矢量的自定义分配器<char>被忽略
- Realloc-在标准分配器中