How to typedef a template class? [duplicate]
Asked Answered
B

2

83

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 ?

Bulla answered 2/8, 2011 at 4:19 Comment(6)
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.Bulla
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.Delainedelainey
what's wrong with the macro implementation?Arietta
@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.Bulla
@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 discussedArteriotomy
It is pretty awful etiquette, IMO, to ask something, give an answer as to why you want to do something, and then, as @Delainedelainey 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.Ivoryivorywhite
M
144

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.

Mccomas answered 2/8, 2011 at 4:23 Comment(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 ?Bulla
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.Mccomas
"The catch is that nobody supports it yet." It's in clang (svn trunk) since a few month.Pyxie
if it's not obvious, this doesn't work in MSVC10SP1Sargasso
This does not work with MSVC 2012, but it does work with MSVC 2013 SP4.Postorbital
F
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.

Fretwell answered 2/8, 2011 at 7:0 Comment(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...Demantoid
@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.Fretwell
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.Fishmonger

© 2022 - 2024 — McMap. All rights reserved.