来自模板参数的扩展参数包(值)

ami*_*421 2 c++ variadic-templates

尝试使用通常的递归方式扩展包:

template<bool first> int func1(int value = 0) {
    return some_func(first, value);
}

template<bool first, bool... args> int func1(int value = 0) {
    return func1<args...>(some_func(first, value) );
}
Run Code Online (Sandbox Code Playgroud)

在编译时递归的最后一步,func1 的调用是不明确的,第一个候选是第一个函数,很明显,在我的情况下有一些具体的专业化:

int func1(int) [bool first = false]

但第二个是

int func1(int) [用 bool first = false; bool ...args = {}]

你看这也是正确的 - 第一个参数之后的空参数集。有什么想法可以防止这种情况吗?

谢谢你

Vit*_*meo 5

通过添加显式second参数来消除基本情况与递归情况的歧义:

template<bool first> int func1(int value = 0) {
    return some_func(first, value);
}

template<bool first, bool second, bool... args> int func1(int value = 0) {
    return func1<second, args...>(some_func(first, value) );
}
Run Code Online (Sandbox Code Playgroud)

魔杖盒示例