我想出了使用模板和部分专业化的解决方案。下面的代码可以解决问题:
// provide both the required types as template parameters
template<bool condition, typename FirstType, typename SecondType>
class License {};
// then do a partial specialization to choose either of two types
template<typename FirstType, typename SecondType>
class License<true, FirstType, SecondType> {
public: typedef FirstType TYPE; // chosen when condition is true
};
template<typename FirstType, typename SecondType>
class License<false, FirstType, SecondType> {
public: typedef SecondType TYPE; // chosen when condition is false
};
class Standard {
public: string getLicense() { return "Standard"; }
};
class Premium {
public: string getLicense() { return "Premium"; }
};
const bool standard = true;
const bool premium = false;
// now choose the required base type in the first template parameter
class User1 : public License<standard, Standard, Premium>::TYPE {};
class User2 : public License<premium, Standard, Premium>::TYPE {};
int main() {
User1 u1;
cout << u1.getLicense() << endl; // calls Standard::getLicense();
User2 u2;
cout << u2.getLicense() << endl; // calls Premium::getLicense();
}
语法看起来不干净,但结果比使用预处理器指令更干净。