C++ Template - Fast Guide Example

Function Template

.H File

template<class ValueType>
std::wstring MyFunction(uint parameter, ValueType template_parameter);

.Cpp File

template<class ValueType>
std::wstring MyClass::MyFunction(uint parameter, ValueType template_parameter)
{
    return (boost::wformat(L"%1%") % template_parameter).str());
}

//Specialization to allow the template to be used in other files
template std::wstring MyClass::MyFunction<bool>(uint parameter, bool template_parameter);
template std::wstring MyClass::MyFunction<int>(uint parameter, int template_parameter);
Written on