c++ - 执行显式模板实例化 (ETI) 的重复方式更少?(没有宏)
问题描述
小例子问题:
// foo.h
template <typename S>
struct Foo
{
template <typename T>
void Bar ();
};
两者S
和T
都可以是char
或int
。'似乎是一个易于维护的列表,但这里是代码:
// foo.cpp
template <typename S>
template <typename T>
void Foo<S>::
Bar ()
{
// uses S and T
}
template class Foo<int>;
template class Foo<char>;
template void Foo<int>::Bar<int>();
template void Foo<int>::Bar<char>();
template void Foo<char>::Bar<int>();
template void Foo<char>::Bar<char>();
只是为了使它成为一个完整的例子:
// main.cpp
#include "foo.h"
int main ()
{
// uses all the Foo/Bar permutations
}
在我的真实示例中,我有 2 个外部类型和 5 个内部类型用于 2 个方法,结果是 2 + 3 * 2 * 5 = 32 个 ETI!
我知道在使用 ETI 时我需要维护一个使用过的参数列表,但我真的需要维护所有的排列吗?