c++ - C++ 模板函数的重载

标签 c++ templates c++11 overloading overload-resolution

我认为下面的代码应该可以工作,但是 g++ 和 clang++ 都返回完全相同的错误(尽管 Visual C++ 2012 不会)。

#include <iostream>
#include <tuple>

template <int N, typename T>
struct A { };

template <typename Tuple>
double result(const Tuple& t, const A<0, typename std::tuple_element<0, Tuple>::type>& a)
{
  return 0;
}

template <typename Tuple>
double result(const Tuple& t, const A<std::tuple_size<Tuple>::value-1,
                                      typename std::tuple_element<std::tuple_size<Tuple>::value-1,Tuple>::type>& a)
{
  return 1;
}

template <typename Tuple, int N>
double result(const Tuple& t, const A<N, typename std::tuple_element<N, Tuple>::type>& a)
{
  return 0.5;
}

int main()
{
  auto a = std::make_tuple(0, 1, 2., 3., 4);
  std::cout << result(a, A<0,int>()) << std::endl;
  std::cout << result(a, A<2,double>()) << std::endl;
  std::cout << result(a, A<4,int>()) << std::endl; // Fails if uncommented
  return 0;
}

错误是由于最后一行以及第二个和第三个 result 函数被认为是等价的。尽管我认为第二个比第三个更合适(就像第一个一样)。

虽然我不确定。谁能告诉我是我错了还是编译器错了?

最佳答案

TLDR; 您的程序无法编译的原因是第二个和第三个重载在重载解析期间同样匹配良好。特别是,两者都不比另一个更专业。因为重载决策无法选择最佳匹配,所以程序格式错误。治愈方法是让 SFINAE 摆脱困境。

问题

14.5.6.2 函数模板的部分排序 [temp.func.order]

2 Partial ordering selects which of two function templates is more specialized than the other by transforming each template in turn (see next paragraph) and performing template argument deduction using the function type. The deduction process determines whether one of the templates is more specialized than the other. If so, the more specialized template is the one chosen by the partial ordering process.

3 To produce the transformed template, for each type, non-type, or template template parameter (including template parameter packs (14.5.3) thereof) synthesize a unique type, value, or class template respectively and substitute it for each occurrence of that parameter in the function type of the template.

对于所有三个重载,第一个合成参数是相等的,并且由于所有参数都被一个一个地考虑,我们可以关注第二个。

您的第一个重载被转换为以下合成的第二个参数

const A<0, typename std::tuple_element<0, Arg1>::type>&

您的第二个重载被转换为以下合成的第二个参数

const A<
        std::tuple_size<Arg1>::value-1, typename        
        std::tuple_element<std::tuple_size<Arg1>::value-1, Arg1>::type
>&

您的第三个重载被转换为以下合成的第二个参数

const A<Arg2, typename std::tuple_element<Arg2, Arg1>::type>&    

14.8.2.4 在部分排序期间推导模板参数 [temp.deduct.partial]

2 Two sets of types are used to determine the partial ordering. For each of the templates involved there is the original function type and the transformed function type. [ Note: The creation of the transformed type is described in 14.5.6.2. — end note ] The deduction process uses the transformed type as the argument template and the original type of the other template as the parameter template. This process is done twice for each type involved in the partial ordering comparison: once using the transformed template-1 as the argument template and template-2 as the parameter template and again using the transformed template-2 as the argument template and template-1 as the parameter template.

很明显,第一个和第二个重载没有第二个模板参数可以推导,因此它们至少与第三个重载一样专业。问题是第三个是否可以从第一个和第二个重载的合成第二个参数中推导出它的 N 参数。

对于第一个重载,N=0 为真,因此第一个重载比第三个更特殊。这就是您的第一个函数调用选择第一个重载的原因。

对于第三个重载,参数推导不会发生,它是一个非推导的上下文:

14.8.2.5 从类型 [temp.deduct.type] 推导模板参数

5 The non-deduced contexts are:

— ...

A non-type template argument or an array bound in which a subexpression references a template parameter.

— ...

这意味着第三个重载至少和第二个重载一样专业。因此,重载决议无法选择一个,程序是病式的。

治愈

只需在 enable_if 中使用非重叠条件进行两个重载(使用 SFINAE)。在这种情况下,这会绕过重载决议。

template <typename Tuple, int N>
typename std::enable_if<N == std::tuple_size<Tuple>::value-1, double>::type
result(const Tuple& t, const A<N, typename std::tuple_element<N, Tuple>::type>& a)
{
  return 1;
}

template <typename Tuple, int N>
typename std::enable_if<N != std::tuple_size<Tuple>::value-1, double>::type
result(const Tuple& t, const A<N, typename std::tuple_element<N, Tuple>::type>& a)
{
  return 0.5;
}

Live Example .

关于c++ - C++ 模板函数的重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19365571/

相关文章:

c++ - 调用 weakref 传递的 Python 函数

c++ - printf 使 setrlimit 不起作用

c++ - 模板类型的重载决策和运算符 << 的问题 - 第 2 部分

c++ - 如何使用 unordered_multimap 在模板类方法中执行 make_pair?

c++ - 修复错误 : taking address of temporary without changing codes

c++ - 在不使用嵌套名称说明符的情况下定义 namespace 的成员的 clang 中是否存在错误

c++ - 根据参数类型推断重载函数的函数类型

c++ - unique_ptr 参数。错误 C2664

c++ - 这种将数据从标准容器移动到共享指针的方法是否正确?

c++ - 如何在条件变量等待时关闭时阻止 C++ 程序崩溃?