Template in cpp: two methods

    xiaoxiao2021-03-26  7

    Method 1


    template <typename _T> bool isBigger(_T a, _T b){ return a > b; }

    This is the most used method.

    Method 2


    template <typename _LayerType> void createLayerFromCaffe(string&);


    #include "function.hpp" template<> void createLayerFromCaffe<int>(string ¶ms) { cout << "create using int" << endl; } template<> void createLayerFromCaffe<float>(string ¶ms) { cout << "create using float" << endl; }


    For the first method, the implementation of template function must be in header file. Since when compiling code, compiler needs to know all the function I/O information.For the second method, there is no _T in function declaration. That means _LayerType is not used. So you can put implementation in cpp file. At the same time, you have to implement all the possible called function type for _LayerType. Although the latter one behaves less-like a template function, but it indeed is template function. Template function provides a method to give the same interface for a series of functions.
    转载请注明原文地址: https://ju.6miu.com/read-500035.html