Timo
Timo

Reputation: 9835

Does this class satisfy the Allocator requirement?

I made a custom allocator, but my code didn't compile on msvc and I'm not sure if my implementation satisfies the Allocator requirement (disregarding actual behavior of function implementations here). Here is a minimal example that reproduces the error on Visual Studio (16.11 P1 and 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 isn't complaining for any major compiler but their msvc version is a little behind I think.

To me this looks like a compiler bug in msvc but I want to make sure before I open a ticket.

This is the compiler output:

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 ==========

Edit

I opened a bug ticket for the msvc devs.

Upvotes: 3

Views: 315

Answers (1)

T.C.
T.C.

Reputation: 137395

It does not.

An allocator rebound to a different value type must be constructible from the original allocator - this is the A a(b) row in the requirements you linked.

Your type fails that requirement.

Upvotes: 3

Related Questions