Ошибка при написании собственного аллокатора c++

Рейтинг: 0Ответов: 1Опубликовано: 20.01.2023

Пытаюсь написать свой аллокатор для вектора:

#include <iostream>
#include <memory>
#include <vector>
template<class T>
class LinearAllocator
{
public :
    LinearAllocator() = default;
    typedef T* pointer;
    typedef const T* const_pointer;
    typedef T value_type;
    template <typename U> 
    struct rebind { 
        using other = LinearAllocator<U>;
    };
    T* allocate(std::size_t count)
    {
        std::cout << "alloc " << count << std::endl;
        return static_cast<T*>(std::malloc(count*sizeof(T)));
    }
    void deallocate(T* ptr, std::size_t count)
    {
        std::cout << "dealloc " << count << std::endl;
        free(ptr);
    }
};
template<class T, class U>
bool operator==(const LinearAllocator<T>& left_, const LinearAllocator<U>& right_)
{
    return false;
}
template<class T, class U>
bool operator!=(const LinearAllocator<T>& left_, const LinearAllocator<U>& right_)
{
    return !(left_ == right_);
}

int main(int argc, char** argv)
{
    std::vector<int, LinearAllocator<int>> vector;
    vector.push_back(5);


}

Если передаю его как шаблонный параметр в вектор, то выдает ошибки:

1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\vector(809,27): message : Ни один конструктор не смог принять исходный тип, либо разрешение перегрузки конструктора неоднозначно
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\vector(806): message : во время компиляции функции-члена класс шаблон "std::vector<int,LinearAllocator<int>>::~vector(void) noexcept"
1>C:\Users\REXHD\source\repos\ConsoleApplication9\ConsoleApplication9.cpp(40): message : выполняется компиляция ссылки на экземпляр шаблон функции "std::vector<int,LinearAllocator<int>>::~vector(void) noexcept"
1>C:\Users\REXHD\source\repos\ConsoleApplication9\ConsoleApplication9.cpp(40): message : выполняется компиляция ссылки на экземпляр класс шаблон функции "std::vector<int,LinearAllocator<int>>"
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\vector(809,25): error C2530: _Alproxy: ссылки должны быть инициализированы
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\vector(810,1): error C3536: _Alproxy: не может использоваться до инициализации
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\vector(810,1): error C2672: "_Delete_plain_internal": не найдена соответствующая перегруженная функция
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\xmemory(989,19): message : может быть "void std::_Delete_plain_internal(_Alloc &,_Alloc::value_type *const ) noexcept"
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\vector(810,9): message : Сбой при специализации функции-шаблона "void std::_Delete_plain_internal(_Alloc &,_Alloc::value_type *const ) noexcept".
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\xmemory(989): message : см. объявление "std::_Delete_plain_internal"
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\vector(810,1): message : Со следующими аргументами шаблона:
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\vector(810,1): message : "_Alloc=int"

Ответы

▲ 0Принят

Нашел ошибку, нужно было доопределить конструктор копирования

template<class U>
    constexpr LinearAllocator(const LinearAllocator<U>&) noexcept {}