先定义如下:
ns.h
template <typename T> // 这个关键字typename, 明显多此一举 inline void PRINT_ELEMENTS(const T& coll,const std::string& optstr="") { std::cout << optstr << endl; for(const auto& ele:coll) { std::cout << ele << ' '; } std::cout << std::endl; }
#include "ns.h" using namespace std; #define GUID_LEN 64 int _tmain(int argc, _TCHAR* argv[]) { vector<string> vf; vf.push_back("one"); PRINT_ELEMENTS(vf,"cpp first template"); }
输出如下:
cpp first template one