这个 class 是否满足 Allocator 要求?

Does this class satisfy the Allocator requirement?

我做了一个自定义分配器,但我的代码没有在 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 Studio19\Preview\VC\Tools\MSVC.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 Studio19\Preview\VC\Tools\MSVC.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 Studio19\Preview\VC\Tools\MSVC.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 Studio19\Preview\VC\Tools\MSVC.29.30129\include\vector(714,25): error C2530: '_Alproxy': references must be initialized
1>C:\Program Files (x86)\Microsoft Visual Studio19\Preview\VC\Tools\MSVC.29.30129\include\vector(715,1): error C3536: '_Alproxy': cannot be used before it is initialized
1>C:\Program Files (x86)\Microsoft Visual Studio19\Preview\VC\Tools\MSVC.29.30129\include\vector(715,9): error C2672: '_Delete_plain_internal': no matching overloaded function found
1>C:\Program Files (x86)\Microsoft Visual Studio19\Preview\VC\Tools\MSVC.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 Studio19\Preview\VC\Tools\MSVC.29.30129\include\xmemory(998): message : see declaration of 'std::_Delete_plain_internal'
1>C:\Program Files (x86)\Microsoft Visual Studio19\Preview\VC\Tools\MSVC.29.30129\include\vector(715,1): message : With the following template arguments:
1>C:\Program Files (x86)\Microsoft Visual Studio19\Preview\VC\Tools\MSVC.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 ==========

编辑

我为 msvc 开发人员打开了一个 bug ticket

没有。

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

您的类型不符合该要求。