Reputation: 3952
I'd like to make a typedef for std::list<std::unique_ptr<>>
, so I could type PtrList<A>
and it would be replaced by std::list<std::unique_ptr<A>>
.
I know the way to do that with #define:
#define PtrList(x) std::list<std::unique_ptr<x>>
But I think typedef would be proper. However I don't know how to realize that. Could someone tell me how to typedef this ?
Upvotes: 2
Views: 2116
Reputation: 24576
In C++11: use a template alias
template <class T>
using PtrList = std::list<std::unique_ptr<T>>;
//later:
PtrList<int> myIntPtrList;
in C++98/03: There is no template alias, so in cases like this you will need a workaround. There is no std::unique_ptr
either, so you obviously have C++11 already, but for the sake of completeness here is a C++03 example with a shared_ptr
(i refuse to use auto_ptr
, regardless of it is "more like unique_ptr
"):
template <class T>
struct PtrList { typedef std::list<boost::shared_ptr<T>> type; };
//later:
PtrList<int>::type myIntPtrList;
Upvotes: 3
Reputation: 20993
In the current version of C++ (C++11) you can using type aliases:
template<class T> using PtrList = std::list<std::unique_ptr<T>>;
Upvotes: 2
Reputation: 27365
First, try not to use #define
to generate code. Except for conditional compilation, #define
should be avoided.
A correct type alias:
template<class T> using PtrList = std::list<std::unique_ptr<T>>;
Example use:
PtrList<int> intPtrList;
Upvotes: 9
Reputation: 409136
How about
template<typename A>
using PtrList = std::list<std::unique_ptr<A>>;
PtrList<SomeStructure> myList;
Upvotes: 5