3

I would like to define a custom vector class that uses std::vector class with custom allocator as below:

template <class T> typedef std::vector<T, MyLib::MyAlloc<T> > my_vector; 

Then, when I ttry to use it as:

 my_vector<std::string> v; 

My g++ 2.95.3 Compiler on Solaris 10 complains stating that

 template declaration of `typedef class vector<T,MyLib::MyAlloc<T1> > my_vector' aggregate `class my_vector<basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> > > v' has incomplete type and cannot be initialized 

Please help me to correct the snippet.

2
  • your error says ....MyLib::MyAlloc<T1> but there is no T1 in the code. If this is really the code and the error message then this is quite strange Commented Dec 20, 2017 at 8:42
  • @tobi303 I am confused too Commented Dec 20, 2017 at 8:43

1 Answer 1

2

C++11 supports this with the "new" type alias syntax:

template <class T> using my_vector = std::vector<T, MyLib::MyAlloc<T> >; 

The "old" form (typedef) cannot be used to create an alias template.


If C++11 or beyond is not an option. The only recourse is a template meta-function:

template <class T> struct my_vector { typedef std::vector<T, MyLib::MyAlloc<T> > type; }; 

Which can be used like this:

my_vector<std::string>::type v; 

Or, since std::vector is a class type:

template <class T> struct my_vector : std::vector<T, MyLib::MyAlloc<T> > {}; 

Which may be used as you originally wished it to be used.

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

4 Comments

Oops, but my compiler in this case is prior C++11, it's g++ 2.95.3
@Dr.DebasishJana - Then the alias can't be used directly. See my edit.
Yes, this is ok for 2.95.3, but for member functions like push_back etc needs to be overridden for my_vector?
@Dr.DebasishJana - No. It's public inheritance.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.