c++ - 如何在 C++ 中通过其值类型专门化迭代器?

标签 c++ templates iterator template-specialization

是否可以通过 value_type 专门化 Iterator 模板参数?

我有一个具有以下原型(prototype)的函数。

template<typename InputIterator>
void f(InputIterator first, InputIterator last);

如果 InputIterator::value_typeSomeSpecificType.

,我想特别处理

最佳答案

您可以使用一些中间结构来获得您需要的部分模板特化。像这样的东西应该可以解决问题

template<typename T, typename V>
struct f_impl
{
  static void f( T first, T last ) {...}; //Default version
};

template<typename T>
struct f_impl<T, SomeSpecificType>
{
   static void f(T first,T last) {...}; //Specialisation
};

template<typename InputIterator> void f(InputIterator first, InputIterator last)
{
  f_impl<
      InputIterator,
      typename std::iterator_traits<InputIterator>::value_type
  >::f(first,last);
};

关于c++ - 如何在 C++ 中通过其值类型专门化迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6273793/

相关文章:

C++ 我怎样才能改进这个模板元程序来返回包含大小的数组?

c++ - SFINAE 检测类型是否被定义

Java iterator.hasNext() 始终为真

c++ - 用 gcc -pg -g 编译后没有写 gmon.out

c++ - Qt 中的 MS Word/ODF 自动化

c++ - 用于检测 arm64 或 armv7 的 xcode 架构宏

c++ - LNK2019 : unresolved external symbol; foreach. h FE_Iterator()

c++ - 分数类增量运算符重载解释

templates - Jekyll 中的画廊通过 YAML header 列表

c - C 中的快速失败迭代器应该如何失败