【问题标题】:partial specialization for iterator type of a specified container type指定容器类型的迭代器类型的部分特化
【发布时间】:2013-04-20 17:37:20
【问题描述】:

我有一个模板结构,它接受一个迭代器类型作为模板参数。 现在我需要为不同容器的迭代器专门化该类。 我试过 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' : 模板参数在部分特化中未使用或可推导出

谁能告诉我为什么这不起作用?以及如何让它发挥作用?

【问题讨论】:

  • 为什么要专攻不同容器的迭代器?可能不会使用特化 vector 迭代器,因为它可能只是 T*。您是否尝试查看 in iterator 是否支持随机访问,在这种情况下您想要 iterator_traits::iterator_category
  • @JohnBandela : 我想扩展迭代器类别,编写自己的 iterator_traits,并使其与 stl 容器兼容

标签: c++ templates partial-specialization


【解决方案1】:

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

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&lt;float&gt;T 应该是什么?

关键是没有理由任何 T 应该存在而Foo&lt;T&gt;::type 是你想要匹配的东西,即使有一个,也没有理由这样做将是独一无二的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 2019-08-30
    • 1970-01-01
    相关资源
    最近更新 更多