64,651
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
template <class T>
void func()
{
}
void func(const char *p)
{
printf("%s\n",p);
}
int main(int argc,char* argv[])
{
func("Hi");
return 0;
}
template<class T>
void fun(T *data)
{
}
template<>
void fun(char *data)
{
//T == char
}
template<>
void fun(int *data)
{
//T == int
}
template<class T> struct Type{
enum { result};
};
template<>struct Type<char>{
enum {result=1};
};
template<>struct Type<int>{
enum {result=2};
};
template<class T> void fun(T data)
{
if(Type<T>::result ==1)
cout<<"type is char"<<endl;
if(Type<T>::result ==2)
cout<<"type is int"<<endl;
}
int main()
{
int a=10;
fun(a);
char b='a';
fun(b);
system("pause");
return 0;
}
#include <iostream>
#include <typeinfo>
#include <string>
template < typename T>
void PrintType(T a)
{
std::cout<<typeid(a).name()<<std::endl;
}
int main()
{
PrintType(9);
PrintType(3.0);
PrintType('2');
PrintType("char*");
PrintType(std::string("string"));
return 0;
}