[英]Template function with container type deduction
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.... 我编写了一个函数来迭代
list
, vector
或任何带有iterator
的字符串,并且该函数在字符串上返回一对相同类型的容器....
I wrote the following, but i doesn't compile, I try to catch the container type as C and the Allocator as A. 我写了以下内容,但我没有编译,我尝试将容器类型捕获为C,将Allocator捕获为A.
Important, I'm using C++98 only. 重要的是,我只使用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)
{
//....
}
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 ? 你知道如何写这个功能吗?
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
. 正如@ Jarod42在评论中指出的那样,上述签名不允许
C
和A
类型扣除。 And it doesn't match your use case anyway; 并且它无论如何都与您的用例不符; for that, use this, which will deduce
C
and A
just fine: 为此,使用这个,这将推断
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)
{
//....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.