我有一個應該基於其包含的數據類型來調用大型數據集的相應功能這個包裝的功能,如:調用相應功能基於一個枚舉,但使用模板
void WrapperFunc(int iRealDataType, int iUseAsDataType)
{
// now call the right function based on both arguments
switch (iRealDataType)
{
case FancyType1:
switch (iUseAsDataType)
{
case CoolType1: DataAnalysisFunc_Fancy1_Cool1(); break;
// etc.
}
// etc.
}
}
到目前爲止,這是通過使用兩個嵌套的switch語句,然後調用每個現有的Real和UseAs數據類型組合的許多專用函數之一來解決的。然而,隨着定義類型數量的增加,維護代碼庫是一場噩夢。所以我決定最終使用模板。如果可以的話,我主要避開它們,但這次他們很好地解決了這個問題。
所以現在不是DataAnalysisFunc_Fancy1_Cool1
我想打電話給DataAnalysisFunc<FancyType1,CoolType1>
昂擺脫了數百名的switch語句的多行的,但我不能這樣使用它,因爲FancyType1
是一個枚舉,而不是類型(例如它是Fancy1
)。
只是爲了澄清 - 我知道這聽起來像一個愚蠢的人爲的例子,但我試圖儘可能簡化問題,以達到它的核心,而不是解釋大量的細節將進入一個很大的更具體的例子。
編輯:我的數據分析功能實際上是CUDA內核 - 這可能會排除一些可能的解決方案。對不起。
「iRealDataType」和「iUseAsDataType」的每個組合是一個有效的組合還是隻是一個子集? – SirGuy
這是一個子集,但如果組合是有效的,則在別處處理。 – PeterK
是你傳遞給上述函數變量或常量的值,還是有時候? – Yakk