有一個模板類,像下面選擇性覆蓋模板類的功能
template<typename K, typename V>
class Db {
public:
Db(const string& dbname, int flags = O_RDWR|O_CREAT);
~Db();
// return true on success, false on failure
bool SimpleRetrieve(const K&, V&) const;
bool Retrieve(const K&, V&) const;
};
同時,希望有SimpleRetrieve(const K&, string &) const;
不同的實現,我如何聲明這樣一個模板類?
試過類似下面,力編譯...
template<typename K, typename V>
class Db {
public:
Db(const string& dbname, int flags = O_RDWR|O_CREAT);
~Db();
// return true on success, false on failure
bool SimpleRetrieve(const K&, V&) const;
template<>
bool SimpleRetrieve(const K&, string&) const;
bool Retrieve(const K&, V&) const;
};
帶上不同的虛擬參數 –
如果模板類型是** not **'std :: string',你是否還希望'SimpleRetrieve'的兩個重載都是「活動的」(即可調用的)?如果模板類型**是** std :: string,那麼應該使用哪個函數呢? –