可变模板,size_t
Variadic templates, size_t
本文关键字:size 更新时间:2023-10-16
如何解决这个问题?
#include <iostream>
using namespace std;
template<size_t I, size_t... T>
void fun()
{
cout<<I<<endl;
fun<T...>();
}
template<size_t I>
void fun()
{
cout<<I<<endl;
}
int main()
{
fun<1, 2>();
fun<3>();
return 0;
}
错误:
|21|error: call of overloaded 'fun()' is ambiguous|
|21|note: candidates are:|
|5|note: void fun() [with unsigned int I = 3u;
unsigned int ...T = {}]| |12|note: void fun() [with unsigned int I =
3u]| In instantiation of 'void fun() [with unsigned int I = 2u;
unsigned int ...T = {}]':| |8|required from 'void fun() [with
unsigned int I = 1u; unsigned int ...T = {2u}]'| |20|required from
here| |8|error: no matching function for call to 'fun()'| |8|note:
candidate is:| |5|note: template<unsigned int I, unsigned int ...T>
void fun()| |5|note: template argument deduction/substitution
failed:| |8|note: couldn't deduce template parameter 'I'|
使可变参数版本只接受两个或多个模板参数。
template<size_t I>
void fun()
{
cout<<I<<endl;
}
template<size_t I1, size_t I2, size_t... T>
void fun()
{
cout<<I1<<endl;
fun<I2, T...>();
}
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 大于65535的C++数组[size]引发不一致的溢出
- 为什么(-1)%vector::size()总是返回0
- 在for循环中使用auto vs decltype(vec.size())来处理字符串的向量
- 循环中的条件:为什么每次都调用strlen(),而vector.size()只调用一次
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- vector.size() 在比较中意外工作
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- 返回 str vs. str.substr(0,str.size()) 在 leetcode 中给了我不同的输出
- 为什么 GCC 不能假设 std::vector::size 在这个循环中不会改变?
- 为什么"(!v.empty())"比"(v.size() >0)"好?
- 迭代器库中的 std::size() 不适用于传递给函数的 C 样式数组
- std::string.size() 未知行为
- 为什么gmp会在这里与"invalid next size"重新定位一起崩溃?
- 为什么我会"Invalid read of size 8"?(瓦尔格林德)
- 从 std::string 到 std::array<char,size> 的 memcopy 额外数据是否是一种未定义的行为?
- 使用 std::size 来自非 const 上下文
- "fast"或"normal"在"free(): invalid next size (fast)"中是什么意思?
- 行 - " vector<int> used(adj.size(), 0); "是什么意思?