考虑到C++中模板的类型,是否可以仅构建代码的某些部分?这将是湖泊:
#include <iostream>
using namespace std;
template<typename T>
void printType(T param)
{
#if T == char*
cout << "char*" << endl;
#elif T == int
cout << "int" << endl;
#else
cout << "???" << endl;
#endif
}
int main()
{
printType("Hello world!");
printType(1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)