2012-08-11 45 views
3

我試圖獲得一些最初使用GCC編譯與MSVC編譯的代碼,並且在代碼中遇到了回調包裝類的問題。我已經提取下面的代碼的關鍵部分:MSVC抱怨函數指針不是編譯時常量

template <typename T_func> 
struct internal_parameter_resolver; 

template <typename R> 
struct internal_parameter_resolver<R()> { 
    typedef R(*type)(); 
}; 

template <typename R, typename P1> 
struct internal_parameter_resolver<R(P1)> { 
    typedef R(*type)(P1); 
}; 

template <typename T_func, typename internal_parameter_resolver<T_func>::type func> 
void bind() { 
    // Create and return instance of class Callback... 
} 

double func1() { return 0.5; } 
int func2(double i) { return 0; } 

int main() { 
    bind<double(), &func1>(); // (Line 23) 
    bind<int(double), &func2>(); // (Line 24) 

    return 0; 
} 

雖然這編譯GCC下細,MSVC 2010提供了以下錯誤信息:

1>c:\users\public\documents\projects\_test\_test\main.cpp(23): error C2975: 'func' : invalid template argument for 'bind', expected compile-time constant expression 
1>   c:\users\public\documents\projects\_test\_test\main.cpp(14) : see declaration of 'func' 
1>c:\users\public\documents\projects\_test\_test\main.cpp(24): error C2975: 'func' : invalid template argument for 'bind', expected compile-time constant expression 
1>   c:\users\public\documents\projects\_test\_test\main.cpp(14) : see declaration of 'func' 

有誰知道爲什麼MSVC認爲一個想法那些函數指針不是編譯時常量?或者是代碼中其他地方的問題(即不是第23行和第24行)?如果這是編譯器中的錯誤,我會歡迎任何有關可能的解決方法的建議。

謝謝!

+0

使用gcc 4.6 &&鏗鏘3.1編譯正常。 – mfontanini 2012-08-11 21:24:07

+0

@mfontanini是的,我知道,這是MSVC給我的問題。 – zennehoy 2012-08-11 21:25:10

回答

2
template <typename T_func, T_func* > 
void bind() { 
    // Create and return instance of class Callback... 
} 

Visual C++不擅長解析間接類型定義,但它更喜歡更多具體類型,如上所述。

以上顯示瞭如何處理直接問題您使用Visual C++。

然而,一個稍微好一點的設計是使用自動模板參數推導:

template <typename T_func > 
void bind(T_func const func) { 
    // Create and return instance of class Callback... 
} 

double func1() { return 0.5; } 
int func2(double i) { return 0; } 

int main() { 
    bind(func1);  // (Line 23) 
    bind(func2);  // (Line 24) 
} 

您可以從std::function獲得函數結果類型,如果你想要的,等等。

+0

哇,真的很簡單(使用T_func *)!不幸的是,我不能使用自動模板參數推導,因爲回調類的實例化需要函數指針作爲模板參數。謝謝! – zennehoy 2012-08-11 21:51:22

1

我看不到任何理由通過func1func2作爲非類型的模板參數。

這是更好地只是將它們作爲參數(而不是模板參數),並讓internal_parameter_resolver特徵推斷其類型:

template <typename T_func> 
struct internal_parameter_resolver; 

template <typename R> 
struct internal_parameter_resolver<R()> { 
    typedef R(*type)(); 
}; 

template <typename R, typename P1> 
struct internal_parameter_resolver<R(P1)> { 
    typedef R(*type)(P1); 
}; 

template <typename T_func> 
void bind(typename internal_parameter_resolver<T_func>::type func) { 
    // Create and return instance of class Callback... 
} 

double func1() { return 0.5; } 
int func2(double i) { return 0; } 

int main() { 
    bind<double()>(func1); // (Line 23) 
    bind<int(double)>(func2); // (Line 24) 

    return 0; 
} 

我沒有VC在手,但應該編譯。

+0

感謝您的建議,不幸的是,回調類的實例化需要函數指針作爲模板參數(它基於http://www.codeproject.com/Articles/136799/Lightweight-Generic-C- Callbacks-or-Yet-Another-Del)。 – zennehoy 2012-08-11 21:54:19

相關問題