简体   繁体   中英

Instantiation of a template function

I did some experiments with C++ templates and this is what I got:

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);

As we can see, instantiations of foo1 requires a re-enumeration of T0 types. Is there in the C++ a way to instantiation the foo1, that uses enumeration of previously created instances of classes? Like

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

I believe a c++ way to do this is using type aliasing. You can have something like:

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);

This is how you can unify the first parameter of your template function instantiation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM