0

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.

ryan
  • 839
  • 4
  • 13
  • because `h` is `E` it will fail to match `B(A&)`. And it won't try to interpret as `G` because there could be any number of aliases that may be appropriate to consider as `A`. I don't think aliases are considered in template deduction at all. – kmdreko Mar 03 '17 at 18:20

1 Answers1

2

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.

Barry
  • 286,269
  • 29
  • 621
  • 977
  • Hi Barry, thanks for the answer. This is what I suspected. For my own edification, is there any section of the standard that clarifies this? – ryan Mar 06 '17 at 14:30
  • @ryan [temp.alias] When a template-id refers to the specialization of an alias template, it is equivalent to the associated type obtained by substitution of its template-arguments for the template-parameters in the type-id of the alias template. – Barry Mar 06 '17 at 14:38