gpt4 book ai didi

c++ - 模板模板参数简单示例

转载 作者:太空宇宙 更新时间:2023-11-04 15:32:43 26 4
gpt4 key购买 nike

首先我在学习模板模板参数,我开始想知道我是否有一个 vector<vector<int>> ,如果我可以制作一个模板来提取类型 int从那里。

但是,在尝试构建示例的过程中,我什至无法使单级模板参数模板函数起作用!

#include <iostream>
#include <vector>

template<
template<class> class C2,
class I
>
void for_2d(const C2<I>& container)
{
for( auto j : container ){
std::cout << j;
}
}

int main() {
std::vector<int> cont;
for_2d(cont);
return 0;
}

这会产生:

17 : <source>:17:5: error: no matching function for call to 'for_2d'
for_2d(cont);
^~~~~~
8 : <source>:8:6: note: candidate template ignored: substitution failure : template template argument has different template parameters than its corresponding template template parameter
void for_2d(const C2<I>& container)
^
1 error generated.
Compiler exited with result code 1

最佳答案

您缺少的是 vector 具有多个模板参数(其中大多数具有默认值)。你需要为此准备你的功能

template< 
template<class...> class C2,
class I
>
void for_2d(const C2<I>& container)
{
for( auto j : container ){
std::cout << j;
}
}

注意 class 之后的点

关于c++ - 模板模板参数简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45871748/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com