1

我编写了一个函数来迭代listvector或者任何带有iteratoron 字符串的函数,并且该函数在字符串上返回一对相同类型的容器......

我写了以下内容,但我没有编译,我尝试将容器类型捕获为 C,将分配器捕获为 A。

重要的是,我只使用 C++98。

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)
{
    //.... 
}

要调用该代码,我使用:

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

您知道如何编写该函数吗?

4

1 回答 1

3

模板模板参数只能涉及​​模板参数,不能涉及模板参数。这应该有效:

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)
{
    //.... 
}

正如@Jarod42 在评论中指出的那样,上面的签名不允许对Cand进行类型推导A。无论如何它都不符合您的用例;为此,使用它,这将推断C并且A很好:

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)
{
    //.... 
}
于 2014-02-12T12:32:02.397 回答