c++ - 是选择模板函数的重载还是仿函数(函数对象)的部分特化

标签 c++ stl template-specialization generic-programming sgi

以下是 g++(STL 的 sgi 版本)的 STL 实现的摘录。我想知道他们为什么使用部分特化而不是函数重载。

template <class InputIterator, class OutputIterator>
struct __copy_dispatch
{
  OutputIterator operator()(InputIterator first, InputIterator last,
                            OutputIterator result) {
    return __copy(first, last, result, iterator_category(first));
  }
};

//If the inputiterator and the outputiterator is all type T
//This is a partial specialization of the generalized version
template <class T>
struct __copy_dispatch<T*, T*>//-----------------------(1)
{
  T* operator()(T* first, T* last, T* result) {
    typedef typename __type_traits<T>::has_trivial_assignment_operator t; 
    return __copy_t(first, last, result, t());
  }
};

//Strictly speaking this is a partial specialization of the last template function
template <class T>
struct __copy_dispatch<const T*, T*>//-----------------(2)
{
  T* operator()(const T* first, const T* last, T* result) {
    typedef typename __type_traits<T>::has_trivial_assignment_operator t; 
    return __copy_t(first, last, result, t());
  }
};


//The generalized version of copy
template <class InputIterator, class OutputIterator>
inline OutputIterator copy(InputIterator first, InputIterator last,
                           OutputIterator result)
{
  return __copy_dispatch<InputIterator,OutputIterator>()(first, last, result);
}

//A overload version
inline char* copy(const char* first, const char* last, char* result) {
  memmove(result, first, last - first);
  return result + (last - first);
}

如果我使用像这样的重载版本会怎样:

#include <iostream>

using namespace std;


template <class InputIterator, class OutputIterator>
OutputIterator copy_dispatch(InputIterator first, InputIterator last,
                            OutputIterator result) {
    cout << "now in first" << endl;
    return result;
}
template <class T>
T* copy_dispatch(T* first, T* last, T* result) {
    cout << "now in second" << endl;
    return 0;
}
template <class T>
T* copy_dispatch(const T* first, const T* last, T* result) {
    cout << "now in third" << endl;
    return 0;
}

int main( void ) {
    int a[]={1,2,3,4,5,6};
    double b[] = {1.0,2.0,3.0,4.0,5.0,6.0};
    int c[]={0,0,0,0,0,0};
    int const d[]={0,0,0,0,0,0};

    copy_dispatch(a,a+6, b);
    copy_dispatch(a, a+6, c);
    copy_dispatch(d, d+6, c);

}

输出是:

now in first
now in second
now in third

似乎它也可以正常工作?

那么还有什么理由使用具有部分特化的仿函数类而不是函数重载

更新

以下是 STL 的 sgi 实现的一些其他摘录:

//sgi 4.5
template<bool>
struct _Destroy_aux
{
  template<typename _ForwardIterator>
    static void
    __destroy(_ForwardIterator __first, _ForwardIterator __last)
    {
      for (; __first != __last; ++__first)
        std::_Destroy(&*__first);
    }
};
 
template<>
struct _Destroy_aux<true>
{
  template<typename _ForwardIterator>
    static void
    __destroy(_ForwardIterator, _ForwardIterator) { }
 
};

//in an old version of sgi 2.9 this is implemented with function overload
template <class ForwardIterator>
inline void
__destroy_aux(ForwardIterator first, ForwardIterator last, __false_type) {
  for ( ; first < last; ++first)
    destroy(&*first);
}
 
template <class ForwardIterator> 
inline void __destroy_aux(ForwardIterator, ForwardIterator, __true_type) {}
 
template <class ForwardIterator, class T>
inline void __destroy(ForwardIterator first, ForwardIterator last, T*) {
  typedef typename __type_traits<T>::has_trivial_destructor trivial_destructor;
  __destroy_aux(first, last, trivial_destructor());

最佳答案

Function template specializations do not participate in overload resolution , 并且类模板无法推断出它们的参数。这导致函数模板重载和普通函数作为部分和显式特化代理的不规则模式。

为了结合两全其美,最通用的代码会执行您在问题中显示的内容

//The generalized version of copy
template <class InputIterator, class OutputIterator>
inline OutputIterator copy(InputIterator first, InputIterator last,
                           OutputIterator result)
{
  return __copy_dispatch<InputIterator,OutputIterator>()(first, last, result);
}

顶层函数模板推导其参数(这有助于用户编写紧凑的代码),内部类模板专门用于各种特殊情况(有助于编译器启用优化)。每个体面的编译器都内联了内部函数对象的顶级函数包装器,因此没有开销。

更新:正如您自己注意到的,技术上,您可以通过用函数模板重载替换部分类模板特化,以及用一个显式类模板特化来实现相同的效果普通函数(而不是允许的显式函数模板特化,如 Sutter 专栏中所述,它不会参与重载决策)。

因为委托(delegate)给类模板函数对象的函数模板在部分和显式特化方面表现得更规律,所以对于库编写者和用户来说维护或在需要时修改就不那么微妙了。这是保持简单的原则。

关于c++ - 是选择模板函数的重载还是仿函数(函数对象)的部分特化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17234857/

相关文章:

c++ - 延迟加载 DLL 错误(未找到模块)

c++ - 如何将 wstring 转换为字符串?

c++ - 函数指针的函数模板特化

C++:使用基类中的仅派生类变量

c++ - 在不使用部分模板函数专门化的情况下对递归 `std::vector<std::vector<...>>` 进行字符串化

c++ - 该程序是否在所有标准系统上显示四套牌(♠♣♥♦)?

导致编译错误的类的 c++ mutex 成员

c++ - 帮忙解决这个问题吗?

c++ - 黑莓 10 : GNU STL

c++ - std::priority_queue:自定义排序而不定义比较器类