免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1963 | 回复: 5
打印 上一主题 下一主题

【求助】用模板识别数组引用的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-01-09 15:14 |只看该作者 |倒序浏览
本帖最后由 mlmzw 于 2012-01-09 15:16 编辑

自己写了一个type_traits,用来获取模板类型
template<class T>
struct type_traits
{
    static const int value = 0;
};

template<>
struct type_traits<std::string>
{
    static const int value = 1;
};

template<>
struct type_traits<const char*>
{
    static const int value = 2;
};

使用如下:
template<class T>
void test(const T& t)
{
    std::cout << type_traits<T>::value;
}

目前遇到的问题是,我调用test("hello")时,每次都会打印0,怎样才能针对数组引用偏特化??

论坛徽章:
0
2 [报告]
发表于 2012-01-09 16:01 |只看该作者

  1. #include <string>
  2. #include <sstream>

  3. template <typename T>class type_traits;

  4. #define TYPE_TRAIT_TYPES(type)                  \
  5.   template<> class type_traits<type>{           \
  6.    public:                                      \
  7.    static std::string info(){ return  #type; }  \
  8.   };

  9. TYPE_TRAIT_TYPES(char)
  10. TYPE_TRAIT_TYPES(int)
  11. TYPE_TRAIT_TYPES(float)
  12. TYPE_TRAIT_TYPES(long)
  13. TYPE_TRAIT_TYPES(double)

  14. template <typename T, int i>class type_traits<T[i]>{
  15. public:
  16.   static std::string info(){
  17.     std::stringstream ss;
  18.     ss<<type_traits<T>::info()<<"["<<i<<"]";
  19.     std::string result;
  20.     ss>>result;
  21.     return result;
  22.   }
  23. };


  24. #include <iostream>
  25. template <typename T>
  26. std::string get_type(const T &t) {
  27.   return type_traits<T>::info();
  28. }

  29. #define print_type(type)                        \
  30.   std::cout<<"type of \e[32m "                  \
  31.   << #type << "\e[0m is: "                      \
  32.   << get_type(type)                             \
  33.   << std::endl;

  34. int main(int argc, char *argv[]) {
  35.   print_type("how are you");
  36.   print_type(13);
  37.   print_type(13.65);   
  38.   return 0;
  39. }
复制代码

论坛徽章:
0
3 [报告]
发表于 2012-01-09 16:05 |只看该作者
这样就行了

  1. template<class T>
  2. void test(T t)
  3. {
  4.     std::cout << type_traits<T>::value;
  5. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2012-01-09 16:09 |只看该作者
AD8018 发表于 2012-01-09 16:05
这样就行了


这样的结果是把数组当成引用了,
而且, c++提供了typeid这个关键字, 这个东西非常有用, 识别类型比模板牛X。

论坛徽章:
0
5 [报告]
发表于 2012-01-09 16:39 |只看该作者
gtkmm 发表于 2012-01-09 16:09
这样的结果是把数组当成引用了,
而且, c++提供了typeid这个关键字, 这个东西非常有用, 识别类型比模 ...


typeid怎么用?

论坛徽章:
0
6 [报告]
发表于 2012-01-09 17:03 |只看该作者

#include <iostream>
#include <typeinfo>

template <typename T>void test(T){
  int value = 0;
  if (typeid(T)==typeid(std::string))
    value=1;
  else if (typeid(T)==typeid(const char*))
    value=2;

  std::cout<<value<<std::endl;
}

int main(int argc, char *argv[]) {
  test("hello world");
  return 0;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP