以下语句是函数重载还是函数部分专用化

Is the following statement a function overload or a function partial specialization?

本文关键字:函数部 专用 重载 语句 函数      更新时间:2023-10-16
template <typename Function> void for_each_element(
  const boost::tuples::null_type&, Function) {}
template <typename Tuple, typename Function> void     
  for_each_element(Tuple& t, Function func) {
    func(t.get_head());
    for_each_element(t.get_tail(),func);
}

给定上面的代码片段,我们是定义重载函数还是部分专用函数?

谢谢

没有函数部分专业化这样的东西。这是一种超载。

例如

template <typename T, typename U>
void foo(T t, U u);
template <typename T>
void foo<T, int>(T t, int u); // Illegal: no partial specialisation of functions
template <typename T>
void foo(T t, int u); // OK

将专业化与重载混合时要小心,因为它并不总是像您认为的那样工作。

重载。你不能部分地专门化函数,即使你可以,你也会有第二对<>括号。

这是一个重载。 功能的部分专业化是不可能的。 如果您尝试部分专用化函数,编译器将抱怨。 在这种情况下,当您到达元组的末尾时,编译器将使用重载解析来选择第一个函数。