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 interprocess 的共享内存和分配器的几个不同选项之间切换,我只需修改 typedef。不幸的是,分配器是模板,所以我无法对要使用的分配器进行类型定义。
有没有办法在 C++ 中实现模板的别名? (明显的 #define ALLOCATOR_T boost::interprocess::adaptive_pool
除外)
最佳答案
是的,(如果我正确理解您的问题)您可以将模板“包装”成如下结构:
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
关于c++ - C++ 模板的别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2697607/