82

How should I typedef a template class ? Something like:

typedef std::vector myVector;  // <--- compiler error

I know of 2 ways:

(1) #define myVector std::vector // not so good
(2) template<typename T>
    struct myVector { typedef std::vector<T> type; }; // verbose

Do we have anything better in C++0x ?

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 2
    I am implementing something where I have some `template class` involved. Initially, I will implement it with standard classes then, I want to move to my custom classes made. So I want that switch should be as simple as changing a `typedef`. – iammilind Aug 02 '11 at 07:45
  • I have to agree with David. Why WOULD you want to alias this? indirection is the leading cause for unreadable and un-maintainable code. However you did give a good reason, "Abstraction". The problem with stl is the API. It prevents specific optimizations. And keeping the same API and thinking you could do better is ill fated. It doesn't take a whole day to write your own growable array. just do it and be done with it. Or just use STL and get on with your life. – Dan Feb 20 '14 at 07:37
  • what's wrong with the macro implementation? – Strin Aug 19 '14 at 00:04
  • 1
    @Strin, nothing wrong technically. Just that the macros are not `namespace/class` bound. They are limitless. If someone want to name their variable as `myVector` it cannot be done. – iammilind Aug 19 '14 at 11:48
  • @user650261: Often understanding the motivation behind a question adds clarity to the problem. Why would you be against a simple request for clarity? Regardless, your comment (and mine) don't belong here, but rather in meta where this has [already been extensively discussed](https://meta.stackexchange.com/questions/11009/how-do-you-get-people-to-answer-the-question-rather-than-argue-the-premise) – Hovercraft Full Of Eels Jul 01 '18 at 18:51
  • 2
    It is pretty awful etiquette, IMO, to ask something, give an answer as to why you want to do something, and then, as @Dan does, explain to the question asker why they are wrong. I mean, what are people supposed to do? Share their entire internal design documents to explain to you that they're doing something valid? It's completely unconstructive. – user650261 Jul 01 '18 at 18:57

2 Answers2

143

Yes. It is called an "alias template," and it's a new feature in C++11.

template<typename T>
using MyVector = std::vector<T, MyCustomAllocator<T>>;

Usage would then be exactly as you expect:

MyVector<int> x; // same as: std::vector<int, MyCustomAllocator<int>>

GCC has supported it since 4.7, Clang has it since 3.0, and MSVC has it in 2013 SP4.

Travis Gockel
  • 26,877
  • 14
  • 89
  • 116
18

In C++03 you can inherit from a class (publically or privately) to do so.

template <typename T>
class MyVector : public std::vector<T, MyCustomAllocator<T> > {};

You need to do a bit more work (Specifically, copy constructors, assignment operators) but it's quite doable.

dascandy
  • 7,184
  • 1
  • 29
  • 50
  • 3
    @iammilind: an upvote rather than protect from downvotes just makes them more probable (*how did this question get to be upvoted!* type of reasoning). I am one of those that is openly against inheriting from STL containers... but in this case there aren't that many alternatives. I guess the best option is rethinking why you would like to do this in the first place... – David Rodríguez - dribeas Aug 02 '11 at 07:36
  • @David Rodriguez agreed on why you would want to. If you heavily use vectors based on another allocator though, template typedefs would have been very welcome. Private inheritance though (ie, implemented-in-terms-of) is fine by me, but only if there's a reason to inherit from it... which there usually isn't. – dascandy Aug 02 '11 at 09:09
  • Perhaps not for std::vector, but in general I can see how it could be handy. Say you have a type like LongName (or a long chain of nested templates); then having a shortcut name could be handy. If you use this type for one or two different T, then you could typedef them. But if you have many types T, then typedef them all could be as long as using the name directly, and yet using the name is painfully long. – bartgol May 06 '15 at 16:35