模板函数的实例化 我用C++模板做了一些实验,这就是我得到的:

模板函数的实例化 我用C++模板做了一些实验,这就是我得到的:,c++,templates,metaprogramming,C++,Templates,Metaprogramming,页眉.hpp template <typename T0> class A { void foo0(T0 t0); template <typename T1> void foo1 (T0 t0, T1 t1); }; 模板 甲级 { 无效foo0(T0-T0); 模板 void foo1(T0-T0,T1-T1); }; source.cpp // foo0 body // ... // foo1 body // ... // And i

页眉.hpp

template <typename T0>
class A 
{
    void foo0(T0 t0);

    template <typename T1>
    void foo1 (T0 t0, T1 t1);
};
模板
甲级
{
无效foo0(T0-T0);
模板
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);
//foo0主体
// ...
//foo1机构
// ...
//以及“float”和“double”类型的类A和foo0的实例化
甲级模板;
甲级模板;
//for foo1使用单独的实例化
//类型“int”的实例化foo1
模板void A::foo1(float t0,int t1);
模板void A::foo1(双t0,int t1);
如我们所见,foo1的实例化需要重新枚举T0类型。C++中有一种实例化PoF1的方法,它使用先前创建的类实例的枚举吗?像

template void A<T0>::foo1<int>(float t0, int t1);
模板void A::foo1(float t0,int t1);

<代码> > p>我相信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);
模板
甲级
{
无效foo0(T0-T0);
使用myType=T0;
模板
void foo1(T0-T0,T1-T1);
};
模板void A::foo1(A::myType t0,int t1);
模板void A::foo1(A::myType t0,int t1);
这就是如何统一模板函数实例化的第一个参数