[英]templated typedef?
我正在使用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;
您可以通過使用C ++模板11型走樣using
如這樣
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 ++標准的未定義行為。
在現實世界中,這在這種特殊情況下無關緊要 - 與基類相比,派生類沒有添加新內容,因此派生類的析構函數只調用基類的析構函數。 無論如何都要小心翼翼地進行妄想。
如果您從未在堆上分配此類變量(並且通常在堆棧上分配矢量變量並作為其他類的成員),則非虛擬析構函數問題不會影響您。
如果您願意將編譯器推向極限,可以使用MACRO完成。 我在為Java的“Future”和“Callable”類實現C ++等價物時做到了這一點。 我們的庫使用引用計數對象,因此“Reference <T>”本身就是一個模板類,其中“T”派生自“ReferencedObject”。
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>>
這樣做的美妙之處在於Macro完全按照“模板類型定義”的要求進行操作,缺點是您不能使用“<>”作為類型參數,並且沒有類型推斷。
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) { .... }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.