显式指定位于模板参数包之后的默认模板参数
Explicitly specify defaulted template parameter located after template parameter pack
为什么在以下情况下我不能显式指定d
?
#include <iostream>
template< typename a, typename b = int, typename ...c, typename d = int >
int
f(a, b, c..., d)
{
return sizeof...(c);
}
int
main()
{
std::cout << f< int, int, int, int/*, char*/ >(1, 2, 3, 4, 'd') << std::endl;
return 0;
}
如果我取消注释最后一个模板参数,那么我期望输出2
,但我得到了一个硬错误:
main.cpp:14:18: error: no matching function for call to 'f'
std::cout << f< int, int, int, int, char >(1, 2, 3, 4, 'd') << std::endl;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:6:1: note: candidate function not viable: requires 6 arguments, but 5 were provided
f(a, b, c..., d)
^
1 error generated.
在这种情况下,否认它的规则是什么?
因为包是贪婪的。因此,char
实际上是c
的一部分,您需要提供与d
相关的参数,由于默认情况,该参数的类型为int
。
相关文章:
- 如何反转整数参数包
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 构造函数参数列表之后的结肠符号
- C 是否允许在变异模板参数之后进行普通参数
- 在计算后,在基类之后调用参数化构造函数
- Cocos2dx.如何在动画序列之后设置回调(传递参数)
- 默认参数之后的可变参数格式是否正确
- 忽略了 NULL 之后的 std::cout 中的参数
- 显式指定位于模板参数包之后的默认模板参数
- 为什么在C++11中的类型参数包之后不允许使用整数值参数包
- 为什么只有一个参数值的函数之后必须为每个参数指定默认值?
- 这是更有效的Qt:构造函数与参数或默认构造函数与setter之后
- 这个指针和所有参数在调用之后都是空的(但在调用之前是ok的)
- 参数之前的常量与函数名称 C++ 之后的常量
- 检查在模板第一个参数之后声明的函数是否为 NULL(传递向量和数组)