Question

I write a function to iterate over list, vector or anything with iterator on string and that function return a pair of the same kind of container on string....

I wrote the following, but i doesn't compile, I try to catch the container type as C and the Allocator as A.

Important, I'm using C++98 only.

template<template<std::string, A> class C, class A>
static std::pair<T<std::string, A>, T<std::string, A> > Check(const T<std::string, A>::iterator &beg, const T<std::string, A>::iterator &end)
{
    //.... 
}

To call that code I use:

vector<string> toCheck; toCheck += "test1", "test2";
pair<vector<string>, vector<string> > found = Check(toCheck.begin(), check.end());

Have you an idea of how to write that function ?

Was it helpful?

Solution

A template template parameter can only involve template parameters, not template arguments. This should work:

template<template<class, class> class C, class A>
static std::pair<C<std::string, A>, C<std::string, A> > Check(const typename C<std::string, A>::iterator &beg, const typename C<std::string, A>::iterator &end)
{
    //.... 
}

As @Jarod42 points out in the comments, the above signature doesn't allow type deduction for C and A. And it doesn't match your use case anyway; for that, use this, which will deduce C and A just fine:

template<template<class, class> class C, class A>
static std::pair<C<std::string, A>, C<std::string, A> > Check(const C<std::string, A> &container)
{
    //.... 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top