首页 > 解决方案 > 执行显式模板实例化 (ETI) 的重复方式更少?(没有宏)

问题描述

小例子问题:

// foo.h
template <typename S>
struct Foo
{
    template <typename T>
    void Bar ();
};

两者ST都可以是charint。'似乎是一个易于维护的列表,但这里是代码:

// 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 时我需要维护一个使用过的参数列表,但我真的需要维护所有的排列吗?

标签: c++templatesc++17explicit-instantiation

解决方案


推荐阅读