一個預C++ 17層的方法是使用局部模板特,喜歡這裏:
template <template T, bool AorB>
struct dummy;
template <typename T, true>
struct dummy {
void MyFunc() { FunctionA<T>(); }
}
template <typename T, false>
struct dummy {
void MyFunc() { FunctionB<T>(); }
}
template <typename T>
void Facade() {
dummy<T, MeetsConditions<T>::value>::MyFunc();
}
如果您需要更多,比2個專業化 - 您可以使用枚舉或整數值,併爲所有需要的枚舉進行專門化。
另一種方法是使用std :: enable_if:
template <typename T>
std::enable_if<MeetsConditions<T>::value, void>::type
MyFunc() {
FunctionA<T>();
}
template <typename T>
std::enable_if<!MeetsConditions<T>::value, void>::type
MyFunc() {
FunctionB<T>();
}
這是一個C++ 17功能 – max66
是的我知道,問題是最新的MSVC不完全支持C++ 17。 –
可能很有意思:[模擬靜態圖片與c11c14](https://baptiste-wicht.com/posts/2015/07/simulate-static_if-with-c11c14.html) – Jarod42