我試圖讓我的模板函數產生一個編譯時錯誤,如果非專業基礎版本實例化。我嘗試了通常的編譯時斷言模式(負數組大小),但即使模板沒有實例化,編譯也失敗了。任何有關如何使其失敗的想法當且僅當基模板函數被實例化時?如何強制使用模板專業化?
template<class Foo> void func(Foo x) {
// I want the compiler to complain only if this function is instantiated.
// Instead, the compiler is complaining here at the declaration.
int Must_Use_Specialization[-1];
}
template<> void func(int x) {
printf("Hi\n");
}
它應該只是'static_assert'(一個關鍵字,而不是一個庫功能)。 – UncleBens 2012-01-13 17:33:50
更正,謝謝! – Useless 2012-01-15 15:38:26