我正在使用 libgc,一个用于 C 和 C++ 的垃圾收集器。 为了使 STL 容器可被垃圾回收,必须使用 gc_allocator。
而不是写
std::vector<MyType>
一个人必须写
std::vector<MyType,gc_allocator<MyType> >
有没有一种方法可以定义类似的东西
template<class T> typedef std::vector<T,gc_allocator<T> > gc_vector<T>;
我前段时间查了一下,发现这是不可能的。但我可能错了,或者可能还有其他方法。
以这种方式定义地图是特别令人不愉快的。
std::map<Key,Val>
成为
std::map<Key,Val, std::less<Key>, gc_allocator< std::pair<const Key, Val> > >
编辑:尝试使用宏后,我发现以下代码破坏了它:
#define gc_vector(T) std::vector<T, gc_allocator<T> >
typedef gc_vector( std::pair< int, float > ) MyVector;
模板化类型定义中的逗号被解释为宏参数分隔符。
所以看来内部类/结构是最好的解决方案。
这是一个关于如何在 C++0X 中完成的示例
// standard vector using my allocator
template<class T>
using gc_vector = std::vector<T, gc_allocator<T> >;
// allocates elements using My_alloc
gc_vector <double> fib = { 1, 2, 3, 5, 8, 13 };
// verbose and fib are of the same type
vector<int, gc_vector <int>> verbose = fib;
您可以使用
using
使用 C++11 模板类型别名,例如像这样
template <typename T>
using gc_vector = std::vector<T, gc_allocator<T>>;
您不能使用“模板化 typedef”,但您可以使用带有内部类型的便利类/结构:
template<typename T>
struct TypeHelper{
typedef std::vector<T,gc_allocator<T> > Vector;
};
然后在你的代码中使用
TypeHelper<MyType>::Vector v;
TypeHelper<MyType>::Vector::iterator it;
地图上也有类似的东西:
template<typename K,typename V>
struct MapHelper{
typedef std::map<K, V, gc_allocator<K,V> > Map;
};
编辑-@Vijay:我不知道是否还有另一种可能的解决方法,这就是我要做的;宏可能会给你一个更紧凑的符号,但我个人不喜欢它:
#define GCVECTOR(T) std::vector<T,gc_allocator<T> >
编辑 - @chmike:请注意,
TypeHelper
解决方案不要求您重新定义构造函数!
您可以公开继承:
template<class T>
class gc_vector<T> : public std::vector<T, gc_allocator<T> >
{
public:
// You'll have to redeclare all std::vector's constructors here so that
// they just pass arguments to corresponding constructors of std::vector
};
这完全解决了你的问题。派生类型可以在可以使用基类型的任何地方使用,并且任何像样的编译器都没有实现开销。
如果您尝试通过指向基类变量的指针删除派生类变量,则 std::vector 具有非虚拟析构函数的事实可能会导致根据 C++ 标准的未定义行为。
在现实世界中,在这种特殊情况下这应该不重要 - 与基类相比,派生类没有添加任何新内容,因此派生类的析构函数只调用基类的析构函数。带着偏执继续前进,无论如何都要小心移植。
如果您从未在堆上分配此类变量(通常在堆栈上分配向量变量并作为其他类的成员),则非虚拟析构函数问题不会影响您。
如果您愿意将编译器推向极限,则可以使用宏来完成。我在为 Java 的“Future”和“Callable”类实现 C++ 等效项时做到了这一点。我们的库使用引用计数对象,因此“Reference
1. Create your template Classes. Mine are:
template<typename T>
class Callable {
private:
public:
virtual T Call() = 0;
};
template<typename T> CountedFuture : public ReferencedObject {
private:
Callable<T>* theTask;
T theResult;
public:
T Get() {
// Run task if necessary ...
if(task) {
theResult = theTask->Call();
delete theTask;
}
return theResult;
}
};
2. In the application code I'm using references, so I define the macro:
#define Future(T) Reference<CountedFuture<T>>
这样做的好处在于,宏完全按照“模板 typedef”执行您想要的操作,缺点是您不能使用“<>”作为类型参数,并且没有类型推理。
3. I can now use the Macro wherever I would use a template, like in functions:
Future(char*) DoSomething() { ... }
bool TestSomething(Future(std::string) f) { .... }