fork download
  1. #include <memory>
  2. #include <iostream>
  3.  
  4.  
  5. namespace wyf {
  6.  
  7. // a stub to get things compiled
  8. template <class>
  9. struct EmptyBaseOptimizationHelper
  10. {
  11. int capacity_;
  12. template <class X, class Y> EmptyBaseOptimizationHelper(X, Y) {}
  13. };
  14.  
  15. template<typename Tp, typename Allocator = std::allocator<Tp>>
  16. class vector {
  17. public:
  18. // linker error occurs when calling this ctor with certain arguments
  19. vector(std::size_t n, const Tp& val, const Allocator& alloc = Allocator());
  20. // omitting other member functions.
  21. private: // three data members to hold the objects and raw memory
  22. Tp* begin_; // using pointer = value_type*;
  23. Tp* end_;
  24. using allocator_type =Allocator;
  25. // Empty Base Optimization Helper class to hold both Allocator and capacity_
  26. EmptyBaseOptimizationHelper<allocator_type> capacity_;
  27. };
  28. // still in vector.hpp, the implemention goes below
  29. template<typename Tp, typename Allocator>
  30. vector<Tp, Allocator>::vector(std::size_t n, const Tp& val, const Allocator& alloc) :
  31. begin_(nullptr), end_(nullptr), capacity_(nullptr, alloc) {
  32. }
  33. }
  34.  
  35. int main() {
  36. wyf::vector<int> v2(10, 20);
  37. std::cout << "It works";
  38. }
  39.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
It works