Method 1
function.hpp
template <typename _
T>
bool isBigger(_
T a, _
T b){
return a > b;
}
This is the most used method.
Method 2
function.hpp
template <
typename _LayerType>
void createLayerFromCaffe(
string&);
function.cpp
#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;
}
Analysis
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