c++templatesgcctype-inferencetemplate-aliases

Are gcc and clang not properly inferring template alias types?


I have the following simple example:

// 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!
}

In this example we are using a template alias in order to have the correct number of template arguments to call the function C(). This is, of course, extracted from the actual situation that led to this problem, so workarounds are not a solution here (nor are they what I'm looking for).

gcc gives:

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>

Is this correct output from gcc according to the standard? To me, it seems strongly like the language should allow proper type inference of template aliases in situations like this.


Solution

  • gcc is correct. The code is ill-formed. Template aliases are transparent - so h is simply of type E<F, int>, and E does not match template <typename> class, as it takes more than one template parameter.

    B<G>(h); works because G does match the template template parameter and no deduction occurs.