I’m learning allocators and I have found that MSVC demand of presence of template constructor for allocator (and g++ and clang do not), here is minimal example of code that triggers error:
#include <map>
#include <vector>
template<class T>
struct ContiguousAllocator
{
public:
using value_type = T;
ContiguousAllocator() noexcept {}
//template<class U>
//ContiguousAllocator(const ContiguousAllocator<U>& v) noexcept
//{}
~ContiguousAllocator() noexcept
{}
T* allocate(std::size_t n)
{
return nullptr;
}
void deallocate(T* p, std::size_t n) {}
};
template<class T, class U>
constexpr bool operator==(const ContiguousAllocator<T>&, const ContiguousAllocator<U>&) noexcept
{
return false;
}
template<class T, class U>
constexpr bool operator!=(const ContiguousAllocator<T>&, const ContiguousAllocator<U>&) noexcept
{
return true;
}
int main(int argc, char* argv[])
{
std::vector<int, ContiguousAllocator<int>> vector;
std::map<int, int, std::greater<int>, ContiguousAllocator<std::pair<const int, int>>> map;
return 0;
}
Uncommenting template constructor for type U
removes error. And the error is following:
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1075): error C2440: 'static_cast': cannot convert from 'ContiguousAllocator<_Newfirst>' to 'ContiguousAllocator<_Newfirst>'
with
[
_Newfirst=std::_Tree_node<std::pair<const int,int>,void *>
]
and
[
_Newfirst=std::_Container_proxy
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1075): note: 'ContiguousAllocator<_Newfirst>::ContiguousAllocator': no overloaded function could convert all the argument types
with
[
_Newfirst=std::_Container_proxy
]
C:UserCodemain.cpp(56): note: could be 'ContiguousAllocator<_Newfirst>::ContiguousAllocator(const ContiguousAllocator<_Newfirst> &)'
with
[
_Newfirst=std::_Container_proxy
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1075): note: 'ContiguousAllocator<_Newfirst>::ContiguousAllocator(const ContiguousAllocator<_Newfirst> &)': cannot convert argument 1 from 'ContiguousAllocator<_Newfirst>' to 'const ContiguousAllocator<_Newfirst> &'
with
[
_Newfirst=std::_Container_proxy
]
and
[
_Newfirst=std::_Tree_node<std::pair<const int,int>,void *>
]
and
[
_Newfirst=std::_Container_proxy
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1075): note: Reason: cannot convert from 'ContiguousAllocator<_Newfirst>' to 'const ContiguousAllocator<_Newfirst>'
with
[
_Newfirst=std::_Tree_node<std::pair<const int,int>,void *>
]
and
[
_Newfirst=std::_Container_proxy
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1075): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1075): note: while trying to match the argument list '(ContiguousAllocator<_Newfirst>)'
with
[
_Newfirst=std::_Tree_node<std::pair<const int,int>,void *>
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1075): note: the template instantiation context (the oldest one first) is
C:UserCodemain.cpp(61): note: see reference to class template instantiation 'std::map<int,int,std::greater<int>,ContiguousAllocator<std::pair<const int,int>>>' being compiled
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includemap(72): note: see reference to class template instantiation 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>' being compiled
with
[
_Kty=int,
_Ty=int,
_Pr=std::greater<int>,
_Alloc=ContiguousAllocator<std::pair<const int,int>>
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1071): note: while compiling class template member function 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::~_Tree(void) noexcept'
with
[
_Kty=int,
_Ty=int,
_Pr=std::greater<int>,
_Alloc=ContiguousAllocator<std::pair<const int,int>>
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includemap(369): note: see the first reference to 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::~_Tree' in 'std::map<int,int,std::greater<int>,ContiguousAllocator<std::pair<const int,int>>>::~map'
with
[
_Kty=int,
_Ty=int,
_Pr=std::greater<int>,
_Alloc=ContiguousAllocator<std::pair<const int,int>>
]
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1075): error C2530: '_Alproxy': references must be initialized
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1076): error C3536: '_Alproxy': cannot be used before it is initialized
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1076): error C2672: '_Delete_plain_internal': no matching overloaded function found
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includexmemory(1112): note: could be 'void std::_Delete_plain_internal(_Alloc &,_Alloc::value_type *const ) noexcept'
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1076): note: Failed to specialize function template 'void std::_Delete_plain_internal(_Alloc &,_Alloc::value_type *const ) noexcept'
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1076): note: With the following template arguments:
C:Program FilesMicrosoft Visual Studio2022CommunityVCToolsMSVC14.40.33807includextree(1076): note: '_Alloc=int'