0

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?

1
  • The code you've shown does not use the Alloc argument, so deleting it doesn't change anything. Commented Feb 16, 2019 at 20:41

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.