C 模板中的模板参数
Template parameters in C++ templates
本文关键字:参数 更新时间:2023-10-16
我正在尝试使用模板模板参数,类似于此处和此处(以及许多其他位置)。
#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);
}
但是
$ 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) {
我缺少什么?
可以肯定的是,这是您要寻找的:
template<template<class, class...> class V, class T, class... Args>
void fn(V<T,Args...>& value)
{
// use value here.
}
简单地称为:
std::vector<int> v;
fn(v);
,在您问之前,是的,您可以使用需要更多参数(例如std::map<>
等)的模板进行操作,只需确保Arg...
涵盖选项,然后指定您关心的强制性。例如:
#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;
}
输出
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> >>]
您可以做另一件事,而不是修改函数定义,即使用模板别名适应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);
}
template < class T, class Alloc = allocator<T> > class vector;
所以应该是:
template<template<class,class> class H, class S,class A>
void f(const H<S,A> &value) {
}
相关文章:
- 如何反转整数参数包
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 函数调用中参数的顺序重要吗
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 使用不带参数的函数访问结构元素
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 如何使用Luacneneneba API正确读取字符串和表参数
- 在派生函数中指定void*参数
- 视图中的参数推导失败:take_while
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- 使用指向成员的指针将成员函数作为参数传递
- 没有名称的C++模板参数
- 如何将enable-if与模板参数和参数包一起使用