C++ 整数\u序列和默认参数值

C++ 整数\u序列和默认参数值,c++,c++11,default-value,c++14,C++,C++11,Default Value,C++14,直接使用默认参数值生成整数序列,如下所示,会导致硬错误(complier clang-3.6): #包括 #包括 #包括 template//比如M-arity,N-类型数 结构测试 { 模板 无效的 运算符()(std::index_sequence=std::make_index_sequence{})常量 { 标准:尺寸指数[M]; 用于(标准:尺寸\u t&m:指数){ m=0; } 对于(;;){ (std::cout{});//难看的解决方法 #恩迪夫 返回退出成功; } 这看起来

直接使用默认参数值生成整数序列,如下所示,会导致硬错误(complier clang-3.6):

#包括
#包括
#包括
template//比如M-arity,N-类型数
结构测试
{
模板
无效的
运算符()(std::index_sequence=std::make_index_sequence{})常量
{
标准:尺寸指数[M];
用于(标准:尺寸\u t&m:指数){
m=0;
}
对于(;;){
(std::cout{});//难看的解决方法
#恩迪夫
返回退出成功;
}
这看起来很奇怪,因为简单的替换可以按预期的方式工作

为什么会这样?为什么在上述情况下不能指定默认参数,但显式指定有效


常量&
和&
附加到参数类型没有任何作用。

模板参数不能从默认参数推导出来。这就是为什么我们通常在构建序列之前委托给帮助器函数的原因:

void operator()() const {
    helper(std::make_index_sequence<M>{});
}

template<std::size_t... i>
void helper(std::index_sequence<i...>) const;
void运算符()()常量{
helper(std::make_index_sequence{});
}
模板
void helper(std::index_序列)常量;

这种限制的根本原因是什么?@Orient标准只是将其列为非推断上下文。直到我阅读了@Orient IMO,我才意识到其基本原理。更好的解释方式是,在创建重载解析的候选集时,编译器需要执行模板参数推断。默认参数如果可以从nts中推断出模板参数,则nts可能会导致意外/不必要的行为。@LightnessRacesinOrbit似乎期望类型完全等效。
std::make_index_sequence::运算符std::index_sequence()const
将是非常有用的功能。@LightnessRacesinOrbit虽然使用了
struct i的实例{constexpr operator std::index_sequence<0,1,2>()const{return{};};
作为默认值没有意义。您正在调用
operator()
没有参数,因此,
i..
被推断为一个空包。您现在需要从
index\u序列
转换为
index\u序列
,除非
M
为0,否则这是不可能的。@0x499602D2有趣的解释,但从clangs错误描述中看不明显。谢谢。
void operator()() const {
    helper(std::make_index_sequence<M>{});
}

template<std::size_t... i>
void helper(std::index_sequence<i...>) const;