【发布时间】:2019-03-29 21:44:54
【问题描述】:
我想将派生类的可变参数模板参数(一组整数)传递给另一个模板类。 我不能修改第二个类,因为它是库的一部分。
我已经想出了如何在编译时存储这些参数(例如常量数组或 integer_sequence),但我不知道如何将这些结构传递给库类。 解决方案可能很明显,但我目前迷失了处理这些可变参数的所有可能性。
我尝试构建一个简单的示例来更好地解释我的问题:
// Example program
#include <iostream>
#include <array>
#include <utility>
// this class has some compile time paramters
template <int... N>
class BaseClass
{
public:
BaseClass(){};
~BaseClass(){};
//one idea to store the parameters using a compile time array
static constexpr std::array<int, sizeof...(N)> _N = {{N...}};
//another idea using a integer sequence type
using _Ni = std::integer_sequence<int, N...>;
};
// this special case of BaseClass hast the parameter 5,6,7,8
class SpecialClass:public BaseClass<5,6,7,8>
{
public:
SpecialClass(){};
~SpecialClass(){};
};
// this class is fixed and can not be modified because it's part of an libary
template <int... N>
class Printer
{
public:
Printer(){};
~Printer(){};
// it can (for example) print its template parameters
void print()
{
int dummy[sizeof...(N)] = { (std::cout << N, 0)... };
}
};
int main()
{
// this obviously works
Printer <1,2,3,4> TestPrinter;
TestPrinter.print();
// this works not
Printer <SpecialClass::_N> TestPrinterSpecialArray;
TestPrinterSpecialArray.print();
// this also works not
Printer <SpecialClass::_Ni> TestPrinterSpecialSequence;
TestPrinterSpecialSequence.print();
return 0;
}
【问题讨论】:
-
你知道你的代码中有很多多余的
;,对吧?