It seems like the compiler is very close to doing what I want (because it calls out my function as a candidate), but I have no idea what I’m doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <list>
using namespace std;
template <class U, template<class U> class T>
void AppendSorted( T<U>& l, U val )
{
typename T<U>::reverse_iterator rt = l.rbegin();
while( ((*rt) > val) && (rt != l.rend()) )
rt++;
l.insert( rt.base(), val );
}
int main( int argc, char* argv[] )
{
list<int> foo;
AppendSorted<int, list<int> >( foo, 5 );
list<int>::iterator i;
for( i = foo.begin(); i != foo.end(); i++ )
{
printf("%d\n",*i);
}
return 0;
}
The error I’m getting is:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:21:43: error: no matching function for call to ‘AppendSorted(std::list<int>&, int)’
test.cpp:21:43: note: candidate is:
test.cpp:8:6: note: template<class U, template<class U> class T> void AppendSorted(T<U>&, U)
This signature
indicates that there will be one concrete type (
class U) and one template (template<class U> class T).This invocation
provides two concrete types,
intandlist<int>.listis a template,list<int>is a concrete type, a template instance, but not a template.Just change the function to accept the concrete type of the collection:
And let the compiler infer the type arguments, instead of specifying them.