gpt4 book ai didi

c++ - 这个类是否满足分配器要求?

转载 作者:行者123 更新时间:2023-12-04 07:30:16 27 4
gpt4 key购买 nike

我做了一个自定义分配器,但我的代码没有在 msvc 上编译,我不确定我的实现是否满足 Allocator requirement (这里忽略函数实现的实际行为)。下面是在 Visual Studio(16.11 P1 和 16.10)上重现错误的最小示例:

#include <memory>
#include <vector>

template <typename T>
class Allocator
{
public:
using value_type = T;

[[nodiscard]]
T* allocate(std::size_t n)
{
return nullptr;
}

void deallocate(T* x, std::size_t n)
{
}

constexpr bool operator==(const Allocator& other) const noexcept
{
return true;
}

constexpr bool operator!=(const Allocator& other) const noexcept
{
return !(*this == other);
}
};

int main()
{
using Alloc = Allocator<int>;
using Vec = std::vector<int, Alloc>;

auto vec = Vec();
}

Godbolt没有提示任何主要编译器,但我认为他们的 msvc 版本有点落后。

对我来说,这看起来像是 msvc 中的编译器错误,但我想在开票之前确认一下。

这是编译器输出:

Build started...
1>------ Build started: Project: Project1, Configuration: Debug x64 ------
1>main.cpp
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(714,27): error C2440: 'static_cast': cannot convert from 'Allocator<int>' to 'Allocator<_Newfirst>'
1> with
1> [
1> _Newfirst=std::_Container_proxy
1> ]
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(714,27): message : No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(711): message : while compiling class template member function 'std::vector<int,Alloc>::~vector(void) noexcept'
1>C:\code\dumpster\Project1\Project1\main.cpp(36): message : see reference to function template instantiation 'std::vector<int,Alloc>::~vector(void) noexcept' being compiled
1>C:\code\dumpster\Project1\Project1\main.cpp(36): message : see reference to class template instantiation 'std::vector<int,Alloc>' being compiled
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(714,25): error C2530: '_Alproxy': references must be initialized
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,1): error C3536: '_Alproxy': cannot be used before it is initialized
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,9): error C2672: '_Delete_plain_internal': no matching overloaded function found
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,1): error C2893: Failed to specialize function template 'void std::_Delete_plain_internal(_Alloc &,_Alloc::value_type *const ) noexcept'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\xmemory(998): message : see declaration of 'std::_Delete_plain_internal'
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,1): message : With the following template arguments:
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.29.30129\include\vector(715,1): message : '_Alloc=int'
1>Done building project "Project1.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

编辑

我打开了一个bug ticket对于 msvc 开发者。

最佳答案

它没有。

重新分配到不同值类型的分配器必须可以从原始分配器构造 - 这是您链接的要求中的 A a(b) 行。

您的类型不符合该要求。

关于c++ - 这个类是否满足分配器要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67973597/

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