vendredi 30 juin 2017

Custom allocator compatibilty

Given the following function signature

void foo(std::vector<int> &bar);

and a custom allocator CustomAlloc, calling foo with a instance of std::vector<int, CustomAlloc> results in

could not convert ‘bar’ from ‘std::vector<int, CustomAlloc<int, [...] >}’ to ‘std::vector<int>’

I am no C++ template guru and please do correct me if I am wrong but my understanding is that std::vector<int> default to std::vector<int, std::allocator<int> > (gcc 7.1.1.-3) and hence defines a completely unrellated type.


This beeing said I am trying to find some solutions to make foo callable using std::vector<int, CustomAlloc> and came with the following.

template<typename Allocator>
void foo(std::vector<int, Allocator> &bar);

I dislike how the allocator specification propagates templates, but I guess that is how the STL works.

So my question is, how would you solve this problem given that foo specically operates on int?

PS: Apologies for the bad title I just couldn't find a good one

Aucun commentaire:

Enregistrer un commentaire