Instead of using
std::vector<Object> ObjectArray;
I would like it to be
MyArray<Object> ObjectArray;
with all the std::vector methods preserved. (like push_back(), reserve(), ...etc)
However, using
typedef std::vector MyArray;
won't work. Should I use template instead? How?
Type alias is a name that refers to a previously defined type (similar to typedef). Alias template is a name that refers to a family of types.
You can use an alias declaration to declare a name to use as a synonym for a previously declared type. (This mechanism is also referred to informally as a type alias). You can also use this mechanism to create an alias template, which can be useful for custom allocators.
C++ is not the alias of C# programming language.
A template is a C++ entity that defines one of the following: a family of classes (class template), which may be nested classes. a family of functions (function template), which may be member functions.
What you would really want is a templated typedef. Unfortunately those are not supported in the current version of C++, but they will be added in C++0x.
For now, here's a possible workaround:
template<class T> struct My {
typedef std::vector<T> Array;
};
My<Object>::Array ObjectArray
Whether or not that is better than simply using std::vector
directly, I'll leave to you to decide.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With