【发布时间】:2016-10-26 14:59:57
【问题描述】:
在 C++ 11 中,我可以构建一个模板,将一系列函数模板显式实例化为数组初始化程序吗?我想要实现的目标(请不要问我为什么需要它,因为这是一个很长的故事)看起来像
// The template I want to instantiate:
template<size_t N> void callbackTemplate(const int dummy) {
// Do something which needs to know 'N'.
}
// The variable I want to assign 'callbackTemplate' instantiations to:
void (*callback)(const int dummy); // This cannot be std::function, because it
// is not defined by me! It is already there and
// must be used as it is.
// This is the set of instantiations I want to prepare:
std::array<decltype(callback), 3> callbackTemplates = {
callbackTemplate<0>, callbackTemplate<1>, callbackTemplate<2>
};
上面的代码按原样工作。我要更改的是 callbackTemplates 数组的初始化。我希望初始化程序成为一个依赖于编译时常量并创建实例化 0..N 的模板。
问题在某种程度上与C++ static const array initialization in template class 有关,但我没有设法“模板化”另一个模板的实例化。
【问题讨论】:
标签: c++ templates variadic-templates