【问题标题】:Does this class satisfy the Allocator requirement?这个类是否满足分配器的要求?
【发布时间】:2021-08-30 13:22:33
【问题描述】:

我制作了一个自定义分配器,但我的代码没有在 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 ==========

编辑

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

【问题讨论】:

  • 您拥有的上帝螺栓链接显示代码正在使用 MSVC 编译。这些编译器错误来自哪里?
  • 从 clang、GCC、MSVC 清理编译 - 实时 - godbolt.org/z/qoYnPYhYb(已注释掉未使用的参数以抑制警告)
  • @NathanOliver 这些错误来自我的机器,它使用的 MSVC 版本比 Godbolt 更新。
  • 您使用的是什么编译器版本和编译器设置?
  • 问题中提到了VS版本(16.10和16.11预览1)。编译器标志基本上是默认标志,/std:c++latest 除外。 cl.exe 在我的机器上是版本19.29.30037,在godbolt 上是19.29.29917

标签: c++ visual-c++ c++20


【解决方案1】:

没有。

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

您的类型不符合该要求。

【讨论】:

  • 哇,就是这样。如果我添加缺少的构造函数,它会编译。不错的收获,谢谢
猜你喜欢
  • 2019-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-24
  • 1970-01-01
  • 1970-01-01
  • 2014-08-17
  • 2014-02-21
相关资源
最近更新 更多