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 ?

6
  • 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 2, 2011 at 7: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, 2014 at 7:37
  • what's wrong with the macro implementation?
    – Strin
    Aug 19, 2014 at 0: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, 2014 at 11:48
  • 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 1, 2018 at 18:57

2 Answers 2

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.

5
  • that's cool, but I am not able to verify it with my gcc4.6 or 4.5 (ideone.com/I8kbA). Are you compiling with MSVC ?
    – iammilind
    Aug 2, 2011 at 4:29
  • 1
    You'll have to wait for C++0x to be accepted first. Then you'll have to wait for compiler writers to actually implement the standard. Aug 2, 2011 at 4:32
  • 1
    "The catch is that nobody supports it yet." It's in clang (svn trunk) since a few month.
    – Arzar
    Aug 2, 2011 at 8:36
  • 1
    if it's not obvious, this doesn't work in MSVC10SP1
    – fusi
    Nov 19, 2014 at 13:18
  • 1
    This does not work with MSVC 2012, but it does work with MSVC 2013 SP4.
    – jorgen
    Jan 23, 2015 at 5:59
19

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.

3
  • 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... Aug 2, 2011 at 7: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 2, 2011 at 9:09
  • Perhaps not for std::vector<int>, but in general I can see how it could be handy. Say you have a type like LongName<ManyFixedTypes,typename T> (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 6, 2015 at 16:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.