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
我的想法是,如果我想在boost进程之间切换共享内存和分配器的几个不同选项,我只需要修改typedef。不幸的是,分配器是模板,所以我不能输入我想要使用的分配器。
有没有办法在C ++中实现模板的别名? (除了显而易见的#define ALLOCATOR_T boost::interprocess::adaptive_pool
)
答案 0 :(得分:17)
是的,(如果我理解你的问题)你可以将模板“包装”成一个结构,如:
template<typename T>
class SomeClass;
template<typename T>
struct MyTypeDef
{
typedef SomeClass<T> type;
};
并将其用作:
MyTypeDef<T>::type
编辑:C ++ 0x支持类似
的内容template<typename T>
using MyType = SomeClass<T>;
Edit2:如果是你的例子
typedef boost::interprocess::adaptive_pool allocator_t;
可以
template<typename T>
struct allocator_t
{
typedef boost::interprocess::adaptive_pool<T> type;
}
并用作
allocator_t<SomeClass>::type
答案 1 :(得分:1)
C ++不支持这一点,但它将在新标准中修复。如果没有非平凡的构造函数(或者如果您乐意编写一些转发构造函数),则可能会从adaptive_pool
派生新的类模板。
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) { }
};
编辑:忘了这个答案。我的投票是@Akanksh。