template<class> struct Printer;
// I want this to match std::vector (and similar linear containers)
template<template<class, class...> class T, class TV, class... TS>
struct Printer<T<TV, TS...>> { ... };
// I want this to match std::map (and similar map-like containers)
template<template<class, class, class...> class TM, class TK, class TV, typename... TS>
struct Printer<TM<TK, TV, TS...>> { ... }
int main()
{
// Both of these match the second specialization, which is only intended
// for std::map (and similar map-like containers)
Printer<std::vector<int>>::something();
Printer<std::map<int, float>>::something();
}
正如從例子中看到,std::vector
和std::map
二者匹配第二專業化。我認爲這是因爲std::vector
的分配器參數匹配到TV
,這是爲std::map
的值。歧義模板特
我怎樣才能匹配std::vector
(和其他線性容器)與第一專業化和std::map
(和其他鍵 - 值容器)與第二個?
請注意,模板參數的數量是一個可怕的方式來解決您的問題:它是脆弱的,取決於你不應該關心的實現細節。 – Yakk
正確的(或至少是標準化的)術語是** Sequential Container **和** Associative Container **。 * nb,這個評論更多的是Google,比你更多。* – Orwellophile