具有参数数量的可变参数专业化的 C++ 模板

C++ template with variadic argument specialization for number of arguments

为接受可变模板参数的 (class) 模板生成特化的最佳方法是什么。

在我的用例中,我有一个 class 接受多个整数,我想要一个专门化的情况,即只有一个整数作为参数,但整数的值可以是任意的。

例如:

template <int a, int ...b>
class c {
    private:
        c<b...> otherClass;
}

template <int a>
class c {
     // Some base case
}

这是否可能,或者实现这种专业化的最简单方法是什么?

Class 模板特化将模板参数附加到 class 名称,如下所示:

template <int a>
class c<a> {};
       ^^^

如果我正确理解你的问题,一个普通的模板专业化就足够了。

#include <iostream>

template <int a, int ...b>
struct c {
    c() { std::cout << "Normal" << std::endl; }
};

template <int a>
struct c<a> {
     c() { std::cout << "Special" << std::endl; }
};

int main() {
    c<1,2> c1;
    c<2> c2;
}