Ivy
Ivy

Reputation: 21

What does a declaration of template look like when using the standard allocator in a template class?

I saw some examples of using allocator as a class member in a template class. Such like:

template <class T, class Alloc = std::allocator<T>> class myVector
{
public:
    ...
protected:
    std::allocator<value_type> _alloc;
    ...
private:
    ...
};

But the code still works when I delete the default-value template argument like template <class T> class myVector. So do we need to add a default-value template argument when we have a allocator as class member? If the answer is yes, why?

Upvotes: 0

Views: 35

Answers (1)

erenon
erenon

Reputation: 19118

The code shown is probably wrong: It should use the provided Alloc type to allocate, instead of hard-coding std::allocator. (And also take advantage of the empty-base-class optimization, to avoid increasing the container size if the allocator is an empty type)

Upvotes: 1

Related Questions