C++函数模板里是否可以获得数据类型?

hjh2005 2011-10-31 09:28:26
比如:
template<class T> void fun(T* data)
{
//因为要调用C的函数所以要判断T是什么类型
//比如 if(T==char) 之类的
}

要怎样获得数据类型? 谢谢
...全文
444 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
hjh2005 2011-11-05
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 stendson 的回复:]

C/C++ code
#include <iostream>
#include <typeinfo>
#include <string>
template < typename T>
void PrintType(T a)
{
std::cout<<typeid(a).name()<<std::endl;
}
int main()
{
PrintType(9);
Pri……
[/Quote]

基础类型也能用typeinfo吗? 以前只在有虚函数的类中用过。
stendson 2011-11-05
  • 打赏
  • 举报
回复
可以,还能用于自定义类型
sdaiver 2011-10-31
  • 打赏
  • 举报
回复
行不通
qq120848369 2011-10-31
  • 打赏
  • 举报
回复
#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;
}
qq120848369 2011-10-31
  • 打赏
  • 举报
回复
函数模板没有用特化的,自己去STL库里找找,绝对没有。

函数只有重载,现在函数模板特化虽然支持了。
yyg990441 2011-10-31
  • 打赏
  • 举报
回复
特化什么?函数模板有什么必要特化?直接用普通函数重载就可以了。
hengshan 2011-10-31
  • 打赏
  • 举报
回复
只能用特化。if是行不通的
iamnobody 2011-10-31
  • 打赏
  • 举报
回复
楼上正解。用特化代替if()
failuer 2011-10-31
  • 打赏
  • 举报
回复
可以使用特化

template<class T>
void fun(T *data)
{

}
template<>
void fun(char *data)
{
//T == char
}
template<>
void fun(int *data)
{
//T == int
}

wpalhm 2011-10-31
  • 打赏
  • 举报
回复
typeid
vilnies 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 stendson 的回复:]

C/C++ code
#include <iostream>
#include <typeinfo>
#include <string>
template < typename T>
void PrintType(T a)
{
std::cout<<typeid(a).name()<<std::endl;
}
int main()
{
PrintType(9);
Pri……
[/Quote]
++++++++++++++++++++++++++
pengzhixi 2011-10-31
  • 打赏
  • 举报
回复
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;
}
cao_julians 2011-10-31
  • 打赏
  • 举报
回复
typeid----关键字哦
UndefinedCoder 2011-10-31
  • 打赏
  • 举报
回复
有必要特化的场合很多,不要一棍子打死。自己去看看boost::bind的实现
chouxiaoya1112 2011-10-31
  • 打赏
  • 举报
回复
那你有模板做什么呢??

做不到吧


pathuang68 2011-10-31
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 stendson 的回复:]

C/C++ code
#include <iostream>
#include <typeinfo>
#include <string>
template < typename T>
void PrintType(T a)
{
std::cout<<typeid(a).name()<<std::endl;
}
int main()
{
PrintType(9);
Pri……
[/Quote]

++
这个好
自由 2011-10-31
  • 打赏
  • 举报
回复
既然有模板特化处理的需求,为什么不用模板类呢?
stendson 2011-10-31
  • 打赏
  • 举报
回复
#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;
}

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧