我想定義一個像任務農業遞歸結構。在這裏,我嘗試了兩個操作數遞歸地可以爲任意數量的操作數工作,因爲它可以嵌套自己。C++自動檢測模板參數?
template <typename T1, typename T2>
class Farm
{
private:
T1 *task1;
T2 *task2;
public:
// save them so that I can use them when invoking call operator
Farm(T1 *_t1, T2 *_t2): task1(_t1), task2(_t2) { }
void operator()()
{
// invoke call operator, meaning a farm could be a task (arbitrary nesting)
(*task1)();
(*task2)();
}
};
int main()
{
... create two pointer(A *a, B *b...)
Farm(a,b); // error: missing template arguments before ‘(’ token
Farm<A, B>(a,b); // in this works, it works
}
問題是自動檢測模板參數在這種情況下不起作用。我在做什麼錯了,我怎麼能通過gcc編譯器實現這個模板參數的隱式檢測。
謝謝!
沒錯。這就是所謂的Object Generator成語:http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Object_Generator – ltjax 2011-06-15 14:49:25
非常感謝您的回答!但是,你能告訴我如何擴展這個_Farm_來爲任意模板參數(使用C++ 0x可變參數模板和可能的元組)。我已經讀了很多,並試圖做到這一點,但不能提出解決方案。 – usman 2011-06-15 15:16:42
@ user600029:這是一個不同的問題。請搜索(或發佈一個新問題)。就個人而言,我還沒有使用C++ 0x足以幫助你,但我相信在這裏有很多人知道如何正確地做到這一點。 – Macke 2011-06-16 07:18:24