0

我有以下简单的例子:

// Function we are interested in using.
template<template<typename> class A>
void B(A<int>&) { }

template<typename C, typename D>
class E { };

// "Adapter" to have only one template argument for E.
template<typename F>
using G = E<F, int>;

int main()
{
  G<int> h;
  B(h); // fails!
  // B<G>(h); // does not fail!
}

在此示例中,我们使用模板别名来获得正确数量的模板参数来调用函数 C()。当然,这是从导致此问题的实际情况中提取的,因此解决方法不是这里的解决方案(也不是我正在寻找的)。

gcc 给出:

test2.cpp: In function ‘int main()’:
test2.cpp:14:6: error: no matching function for call to ‘B(G<int>&)’
   B(h);
      ^
test2.cpp:2:6: note: candidate: template<template<class> class A> void B(A<int>&)
 void B(A<int>&) { }
      ^
test2.cpp:2:6: note:   template argument deduction/substitution failed:
test2.cpp:14:6: error: wrong number of template arguments (2, should be 1)
   B(h);
      ^
test2.cpp:1:35: note: provided for ‘template<class> class A’
 template<template<typename> class A>

根据标准,这是来自 gcc 的正确输出吗?对我来说,在这种情况下,该语言似乎应该允许对模板别名进行适当的类型推断。

4

1 回答 1

2

gcc 是正确的。代码格式不正确。模板别名是透明的 - 所以h只是 type E<F, int>,而E不是 match template <typename> class,因为它需要多个模板参数。

B<G>(h);有效,因为G确实匹配模板模板参数并且没有扣除。

于 2017-03-03T18:26:19.220 回答