如何专门化Iterator的值类型,在C ++? [英] How to specialize Iterator by its value type, in C++?

查看:129
本文介绍了如何专门化Iterator的值类型,在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 value_type

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

我想特别处理 InputIterator :: value_type SomeSpecificType。

推荐答案

以获得您需要的部分模板专业化。类似这样的东西应该做的

You can use some intermediate structs to get the partial template specialisation that you need. Something like this should do the trick

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);
};

这篇关于如何专门化Iterator的值类型,在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆