我用C ++模板做了一些实验,这就是我得到的:
header.hpp
template <typename T0>
class A
{
void foo0(T0 t0);
template <typename T1>
void foo1 (T0 t0, T1 t1);
};
source.cpp
// foo0 body
// ...
// foo1 body
// ...
// And instantiations of class A and foo0 for types "float" and "double"
template class A<float>;
template class A<double>;
// for foo1 uses separately instantiations
// instantiation foo1 for type "int"
template void A<float>::foo1<int>(float t0, int t1);
template void A<double>::foo1<int>(double t0, int t1);
我们可以看到,foo1的实例化需要重新枚举T0类型。 C ++中是否有一种实例化foo1的方法,该方法使用先前创建的类实例的枚举?喜欢
template void A<T0>::foo1<int>(float t0, int t1);
答案 0 :(得分:2)
我相信使用这种方法的C ++方法是使用类型别名。您可以输入以下内容:
template <typename T0>
class A
{
void foo0(T0 t0);
using myType = T0;
template <typename T1>
void foo1(T0 t0, T1 t1);
};
template void A<float>::foo1<int>(A::myType t0, int t1);
template void A<double>::foo1<int>(A::myType t0, int t1);
这是如何统一模板函数实例化的第一个参数。