gpt4 book ai didi

c++ - 确认此标准库错误与 MSVC 2015 RC 中的分配器有关

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:37:24 26 4
gpt4 key购买 nike

这是一个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.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>::deallocate(int *,unsigned int)”:无法将参数 1 从“std::_Container_proxy *”转换为“int *”
无法将参数 1 从“std::_Wrap_alloc>”转换为“const aligned_allocator &”

bool 问题:这是一个错误吗?如果是,我将 ( try ) 提交。

[编辑:如评论中所述,这只发生在 Debug模式下。在 Release模式下,它编译并执行得很好。][编辑:更简单的例子]

最佳答案

Boolean question: is this a bug?

错误


虽然这里 MSVC 给出的模板错误非常无用,但这里的错误是我的(令人放心,因为这个版本的标准库今天发布了)。

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

这里缺少的组件是复制构造函数之一(无法自动生成的模板)。这仅在 rebind 结构被定义时出现,因为 rebind 结构覆盖父类中的相同结构(由于它在父类中,最终导致 < em>parent 的 复制构造函数被调用,所以没有问题(除了它在技术上是错误的)。


这里有趣的是错误直到现在才发生。正如我所说,GCC、Clang 和 MSVC 2013 都喜欢它(即使使用它们各自的 Debug模式)。只是这些都没有调用模板复制构造函数。尽管如此,它由标准指定的,所以再次强调,错误最终是我的。

祝贺 MSVC 编译器团队,抱歉打扰了! :D

关于c++ - 确认此标准库错误与 MSVC 2015 RC 中的分配器有关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31498391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com