Alias ​​for C ++ template?

typedef boost::interprocess::managed_shared_memory::segment_manager segment_manager_t; // Works fine, segment_manager is a class typedef boost::interprocess::adaptive_pool allocator_t; // Can't do this, adaptive_pool is a template 

The idea is that if I want to switch between boost interprocess between several different options for shared memory and allocators, I just change the typedefs. Unfortunately, dispensers are templates, so I cannot typedef the dispenser I want to use.

Is there a way to get a template alias in C ++? (Except for the obvious #define ALLOCATOR_T boost::interprocess::adaptive_pool )

+6
c ++ typedef templates
source share
2 answers

Yes, (if I understand your question correctly), you can "wrap" the template in a structure like:

 template<typename T> class SomeClass; template<typename T> struct MyTypeDef { typedef SomeClass<T> type; }; 

and use it like:

 MyTypeDef<T>::type 

Edit: C ++ 0x will support something like

 template<typename T> using MyType = SomeClass<T>; 

Edit2: In the case of your example

 typedef boost::interprocess::adaptive_pool allocator_t; 

may be

 template<typename T> struct allocator_t { typedef boost::interprocess::adaptive_pool<T> type; } 

and used as

 allocator_t<SomeClass>::type 
+17
source share

C ++ does not support this, although it will be fixed in the new standard. You can get away with getting a new class template from adaptive_pool if there are no non-trivial constructors (or if you are happy to write several forwarding constructors).

 template <class T> class allocator_t : public adaptive_pool<T> { public: // Just making up a forwarding constructor as an example. I know nothing // anything about adaptive_pool. allocator_t(int arg1, float arg2) : adaptive_pool<T>(arg1, arg2) { } }; 

EDIT: Forget about this answer. My voice goes to @Akanksh.

+1
source share

All Articles