7

Is it possible to use typedef on a std container without specializing it?

Code like this works:

typedef std::vector<int> intVector;

But for this code:

template <typename T>
typedef std::vector<T> DynamicArray<T>;

I get an error:

template declaration of 'typedef'

It is possible to do this in C++??

dtech
  • 47,916
  • 17
  • 112
  • 190
  • Considering that all you're basically doing is renaming `std::vector`, how untasteful would it be to use `#define DynamicArray std::vector`? – suszterpatt Apr 27 '12 at 14:07
  • @suszterpatt - true, but I try to keep it modern and avoid usage of the unsafe macros :) – dtech Apr 27 '12 at 14:19

4 Answers4

11

Yes, in C++11.

template <typename T>
using DynamicArray = std::vector<T>;

(Not that you should use this exact alias.)

Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224
  • It doesn't seem to be working in Visual Studio 2010 SP1. I get "unrecognisable template declaration/definition" I suppose it is not supported by MS yet? – dtech Apr 27 '12 at 13:55
  • 1
    @ddriver: No, it's not. And not by VC11, either, AFAIR. If you want C++11, use GCC or Clang. – Cat Plus Plus Apr 27 '12 at 13:58
  • I found a workaround for VS by simply inheriting vector. Please see the answer I added and shed some light if this is not a good idea. Thanks! – dtech Apr 27 '12 at 15:03
6

If your compiler support c++11:

template <typename T>
using DynamicArray = std::vector<T>;

otherwise (c++98 or older) you can use a help structure like the following

template<typename T>
struct DynamicArray
{
  typedef std::vector<T> type;
};

and then use it as

DynamicArray<int>::type my_array;

Inheriting from std::vector is a possible solution but be aware that STL containers do not have virtual destructor. i.e.:

template <typename T>
struct DynamicArray: vector<T> { ... };

int main() {
  vector<int>* p = new DynamicArray<int>();
  delete p; // this is Undefined Behavior
  return 0;
}
log0
  • 10,489
  • 4
  • 28
  • 62
  • Yes, but why would I dynamically allocate a vector when it is using dynamic memory allocation for its data internally? – dtech Apr 27 '12 at 15:31
  • Also, can't I just write a destructor myself, I think the vector's destructor should be responsible for its data, I only need to take care of extra stuff I added. I might be wrong thou, I am not too good with C++. – dtech Apr 27 '12 at 15:35
  • I mean, sure, there might be problems with polymorphic usage, but since the intent is to just use the derived class I don't think there will be any problems due to the lack of virtual destructor. I mean it is not like I am going to call delete on a pointer to vector which actually points to a DynamicArray, which also has no extra dynamically allocated members to require extra care. Please correct me if I am wrong. Thanks! – dtech Apr 27 '12 at 15:48
  • @ddriver You are right, if you are sure you never delete such a pointer you are good. – log0 Apr 27 '12 at 16:10
  • and if I delete from a vector pointer, what would the implications of that be if I don't have any extra members in my derived class? – dtech Apr 27 '12 at 16:17
  • It is undefined behavior as per the standard, so the implications depend on your compiler/platform/... . It might work, but unfortunately it may also delete nothing ... – log0 Apr 27 '12 at 18:15
  • Probably better if add which way is recommended? By 'using ' or by 'struct'.. – Michael Jul 14 '20 at 08:55
  • @Michael `using` is the recommended way. The other is for when your compiler does not support c++11 – log0 Jul 15 '20 at 11:14
3

This syntax is invalid in C++, there is no feature like a "template typedef".

template <typename T>
typedef std::vector<T> DynamicArray<T>;

However, C++11 introduces a template alias syntax that is almost like this:

template <typename T>
using DynamicArray =  std::vector<T>;

In C++03 you can use a template metafunction like:

template<class T>
struct DynamicArray
{
    typedef std::vector<T> type;
};
PlasmaHH
  • 15,673
  • 5
  • 44
  • 57
2

The common solution (if you're not using C++ 11) is to do this:

template<class T>
struct DynamicArray
{
    typedef std::vector<T> Type;
};

And use it as DynamicArray<Something>::Type.

Andreas Brinck
  • 51,293
  • 14
  • 84
  • 114