首页 文章

指定容器类型的迭代器类型的部分特化

提问于
浏览
2

我有一个模板结构,它接受模板参数的Iterator类型 . 现在我需要为不同容器的迭代器专门化该类 . 我试过用std :: vector

template<typename Iterator>
struct AC {

};

template<typename T, typename Alloc>
struct AC<typename std::vector<T, Alloc>::iterator> { //this doesn't work

};

但我得到了这个编译错误(VS11):'T':模板参数在部分特化中未使用或可推导

有人可以告诉我为什么这不起作用?以及如何使它工作?

1 回答

  • 2

    您无法推断嵌套 :: 的左侧类型 . 的确,你的问题毫无意义 . 考虑这个更简单的反例:

    template <typename> struct Foo;
    template <> struct Foo<bool> { typedef float type; };
    template <> struct Foo<char> { typedef float type; };
    
    template <typename> struct DoesntWork;
    
    template <typename T> struct DoesntWork<typename Foo<T>::type> { };
    

    现在,如果我说 DoesntWork<float>T 应该是什么?

    关键是没有理由存在 Foo<T>::type 是你要匹配的东西 Foo<T>::type ,即使有一个,也没有理由说它是唯一的 .

相关问题