Template in cpp: two methods

Method 1

function.hpp

template 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

  1. 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.
  2. 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.
  3. 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.

你可能感兴趣的:(opencv源码)