模板模板参数:没有找到匹配的调用
template template arguments: no matching call found
#include <vector>
using std::vector;
template<template<typename> class x>
void test() {
x<int> a;
a.push_back(1);
}
int main() {
test<vector>();
return 0;
}
由于某种原因,我没有得到匹配调用错误,尽管我的期望。为什么会发生这种情况?
std::vector
不接受一个参数,而是接受两个参数(有Allocator
),因此它不能与只接受一个参数的模板匹配。您需要更改为:
template <template <typename, typename> class x>
// or:
template <template <typename... > class x>
您还需要更改函数的返回类型,因为x<int>
不太可能是void
。
请注意,如果您使用具有两个typename
的版本,则需要在返回语句中指定每个参数(例如x<int, std::allocator<int>>
),这就是为什么您应该选择可变版本(typename...
)。
std::vector模板如下:
template<
class T,
class Allocator = std::allocator<T>
> class vector;
所以你有typename T和allocator,所以正确的代码应该是:
template<template<typename,typename> class x>
void test() {
x<int, std::allocator<int>>();
}
相关文章:
- 为什么即使我调用参数化构造函数也会调用默认构造函数?
- 什么是仅调用一次并调用参数的控制台应用
- 调用参数排列不变函数 f(i++, i++)
- 调用参数不是原子参数的函数是此代码引发异常的原因吗?
- 如何使可调用参数的签名在C++模板中可见?
- x64 函数调用参数推送/移动顺序 (MSVC)
- 如何调用参数由另一个函数生成的函数?
- 从从可调用参数创建的线程对象参数移动构造 C++11 线程
- 正确调用参数化 std::函数
- 是否允许在调用参数中使用类型名 T
- 我不知道如何调用参数为节点类型的函数
- 函数调用参数列表 (C++) 中的空格
- Valgrind 错误:系统调用参数 epoll_pwait(sigmask) 指向不可寻址的字节
- 这C++代码如何让我调用参数数量错误的函数
- 在计算后,在基类之后调用参数化构造函数
- NPAPI调用参数包装
- 在类组合中调用参数化构造函数
- 为什么在创建类对象数组时不能调用参数化构造函数?
- 如何在 for 循环中调用参数化构造函数
- 如何在PIMPL设计模式中调用参数化构造函数