Quantcast
Viewing all articles
Browse latest Browse all 22113

Custom allocator not correctly called in MSVC

I was playing around with custom allocator, but there is nothing printed using MSVC(Microsoft (R) C/C++ Optimizing Compiler Version 19.26.28805 for x86), while there is in GCC / Clang.The comment part needs to be uncommented when using GCC/Clang, but it will stop MSVC to compile, and I don't know if I am doing anything wrong here.

I found the issue here, but it seems the issue is still not resolved in GCC10

#include <iostream>#include <vector>template<typename T>struct MyAllocator :std::allocator<T>{    auto allocate(size_t size)    {        std::cout << "Allocating: "<<size<< " elements\n";        return new T[size];    }    void deallocate(T* ptr, size_t size)    {        std::cout << "Free: "<<size<<" elements\n";        delete[] ptr;    }    /*uncomment the following for GCC or clang*/    /*template <typename U>    struct rebind    {        using other = MyAllocator<U>;    };*/};int main(){    std::vector<int, MyAllocator<int>> v{ 1, 2, 3, 4, 5 };    std::vector<size_t, MyAllocator<size_t>> v2{ 1, 2, 3, 4 };    std::cin.get();}

EDIT: With all the compilers, I am using C++17 standard


Viewing all articles
Browse latest Browse all 22113

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>