[英]How to provide a default parameter argument when the type of that parameter is a template type?
template <class V, class K>
class Pair {
public:
Pair(const K& key, const V& value = initial) { // what should "initial" be here?
// ...
}
}
例如,如果我這樣使用 class:
int main() {
Pair<int, std::string> p1(21); // p1 should be {21, ""} as the default value of a string is "".
Pair<int, double> p2(20); // p2 should be {20, 0.0} assuming the default value of a double is 0.0
}
我怎樣才能做到這一點?
像這樣嘗試V()
:
template <class K, class V>
class Pair {
public:
Pair(const K& key, const V& value = V()) { }
};
或者:
template <class K, class V>
class Pair {
public:
Pair(const K& key, const V& value = {}) { }
};
請注意,默認構造函數是必需的(無需參數即可調用)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.