[英]Template parameters in C++ templates
I am trying to use template template parameters, similar to what is done here and here (and many other places). 我正在尝试使用模板模板参数,类似于此处和此处 (以及许多其他地方)所做的操作。
#include <vector>
template<template<class> class A, class B>
void f(A<B> &value) {
}
int main() {
std::vector<int> value;
f<std::vector, int>(value);
}
But 但
$ g++-4.8 -std=c++0x base64.cpp
base64.cpp: In function ‘int main()’:
base64.cpp:9:23: error: no matching function for call to ‘f(std::vector<int>&)’
f<std::vector, int>(value);
^
base64.cpp:9:23: note: candidate is:
base64.cpp:4:6: note: template<template<class> class H, class S> void f(const H<S>&)
void f(H<S> &value) {
What am I missing? 我错过了什么?
Pretty sure this is what you're looking for: 很确定这就是你要找的东西:
template<template<class, class...> class V, class T, class... Args>
void fn(V<T,Args...>& value)
{
// use value here.
}
Invoked simply as: 简单地调用为:
std::vector<int> v;
fn(v);
And before you ask, yes, you can do it with templates that need more parameters (like std::map<>
, etc), Just make sure Arg...
covers the optionals and you specify the mandatory ones you care about. 在你问之前,是的,你可以用需要更多参数的模板(比如std::map<>
等)来做,只要确保Arg...
涵盖了选项,你指定你关心的强制性。 Such as: 如:
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <unordered_map>
#include <cstdlib>
template< template<class, class...> class V, class T, class... Args>
void fn_seq(const V<T,Args...>& value)
{
std::cout << __PRETTY_FUNCTION__ << '\n';
// use value here.
}
template< template<class, class, class...> class C, class K, class V, class... Args>
void fn_assoc(const C<K,V,Args...>& value)
{
// use value here.
std::cout << __PRETTY_FUNCTION__ << '\n';
}
int main()
{
std::vector<int> vec;
fn_seq(vec);
std::list<double> lst;
fn_seq(lst);
std::map<int, float> m;
fn_assoc(m);
std::unordered_map<long, long> um;
fn_assoc(um);
return EXIT_SUCCESS;
}
Output 产量
void fn_seq(const V<T, Args...> &) [V = vector, T = int, Args = <std::__1::allocator<int>>]
void fn_seq(const V<T, Args...> &) [V = list, T = double, Args = <std::__1::allocator<double>>]
void fn_assoc(const C<K, V, Args...> &) [C = map, K = int, V = float, Args = <std::__1::less<int>, std::__1::allocator<std::__1::pair<const int, float> >>]
void fn_assoc(const C<K, V, Args...> &) [C = unordered_map, K = long, V = long, Args = <std::__1::hash<long>, std::__1::equal_to<long>, std::__1::allocator<std::__1::pair<const long, long> >>]
There is another thing you can do instead of modifying the function definition, ie use template alias to adapt std::vector
: 还有一件事你可以做而不是修改函数定义,即使用模板别名来适应std::vector
:
#include <vector>
#include<iostream>
template<
template<class> class A,
class B
>
void f(A<B> &value) {
for( auto & x : value ) {
std::cout << x << std::endl;
}
}
template< class T >
using VectorAdapter = std::vector<T>;
int main() {
VectorAdapter<int> value {1, 2, 3};
f(value);
}
Vector has 2 template parameters, (the second one is almost never used and has a default allocator type) Vector有2个模板参数,(第二个几乎从未使用过,并且有一个默认的分配器类型)
template < class T, class Alloc = allocator<T> > class vector;
So it should be: 所以它应该是:
template<template<class,class> class H, class S,class A>
void f(const H<S,A> &value) {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.