也许我遗漏了一些明显的东西,但是以下编译并运行,我不确定为什么。我知道this https://stackoverflow.com/questions/27303283/clang-vs-gcc-variadic-template-parameter-pack-followed-by-parameter-with-defau,但在下面的示例中,参数包和默认参数的位置颠倒了。这是否违反了默认参数必须出现在最后的规则?参数包不能有默认值。
#include <iostream>
#include <string>
#include <tuple>
template<typename ... Ts>
struct Test
{
int i;
std::string str;
Test(int _i = 0, Ts&& ... _ts)
:
i(_i),
str(std::get<0>(std::forward_as_tuple(std::forward<Ts>(_ts)...)))
{}
};
int main()
{
Test<std::string> t(1, "huh??");
std::cout << "t.i = " << t.i << ", t.str = " << t.str << "\n";
return 0;
}
这会产生
t.i = 1, t.str = huh??
从 8.3.6 ([dcl.fct.default])/4 开始: https://timsong-cpp.github.io/cppwp/n3337/dcl.fct.default
对于非模板函数,可以稍后添加默认参数
同一作用域中的函数声明。声明于
不同的作用域具有完全不同的默认参数集。
也就是说,内部作用域中的声明不会获取默认参数
来自外部作用域中的声明,反之亦然。在给定函数中
声明,参数后面的每个参数都带有默认值
参数应具有在此或之前提供的默认参数
声明或应为函数参数包。默认参数
不得由稍后的声明重新定义(甚至不能对相同的声明进行重新定义)
价值)。
[ 例子:
void g(int = 0, ...); // OK, ellipsis is not a parameter. So it can follow a parameter with a default argument
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)