从一个模板函数聊聊模板函数里面如何获得T的名字

写了个小程序,遇到点问题.总结总结,学习学习

 1 #include<vector>
 2 #include<iostream>
 3 #include<typeinfo>
 4 
 5 using namespace std;
 6 
 7 enum TYPE 
 8 {
 9     T_INT,
10     T_BOOL,
11     T_CHAR,
12 };
13 
14 template <typename T>
15 void printTime( TYPE type )
16 {
17     clock_t t;
18     vector<T> vc;
19     t = clock();
20     for (int i = 0; i < 1000; i++)
21     {
22         for (int j = 0; j < 1000; j++)
23         {
24             //vc.push_back( reinterpret_cast<T>(0) );
25             vc.push_back( 0 );
26         }
27     }
28     t = clock() - t;
29     
30     switch (type)  //在这里要根据T打印不同的内容
31     {
32         case T_INT:
33             cout << "'vector<int>::push_back(true)' 1000000 times cost: " << t << endl;
34             break;
35         case T_BOOL:
36             cout << "'vector<bool>::push_back(true)' 1000000 times cost: " << t << endl;
37             break;
38         case T_CHAR:
39             cout << "'vector<char>::push_back(true)' 1000000 times cost: " << t << endl;
40             break;
41     }
42 
43     t = clock();
44     for (int i = 0; i < 1000; i++)
45     {
46         for (int j = 0; j < 1000; j++)
47         {
48             T value = vc[i * 1000 + j];
49         }
50     }
51     t = clock() - t;
52     switch (type)  //在这里要根据T打印不同的内容
53     {
54         case T_INT:
55             cout << "'vector<int>::operator[]' 1000000 times cost: " << t << endl;
56             break;
57         case T_BOOL:
58             cout << "'vector<bool>::operator[]' 1000000 times cost: " << t << endl;
59             break;
60         case T_CHAR:
61             cout << "'vector<char>::operator[]' 1000000 times cost: " << t << endl;
62             break;
63     }
64 }
65 int main()
66 {
67     printTime<int>(T_INT);
68     printTime<bool>(T_BOOL);
69     printTime<char>(T_CHAR);    
70     return 0;
71 }
 
 

上面的打印结果是可行的,不过需要用一个枚举感觉很不爽啊..

结果常识了模板和宏结合的方法,结果没打印出理想结果.原因是 宏是先于模板展开的

看程序:

 1 #include<vector>
 2 #include<iostream>
 3 #include<typeinfo>
 4 
 5 using namespace std;
 6 
 7 #define TOSTRING(x) #x
 8 
 9 template <typename T>
10 void printTime()
11 {
12     clock_t t;
13     vector<T> vc;
14     t = clock();
15     for (int i = 0; i < 1000; i++)
16     {
17         for (int j = 0; j < 1000; j++)
18         {
19             //vc.push_back( reinterpret_cast<T>(0) );
20             vc.push_back( 0 );
21         }
22     }
23     t = clock() - t;
24     cout << "'vector<"<<TOSTRING(T)<<">::push_back(true)' 1000000 times cost: " << t << endl;
25 
26     t = clock();
27     for (int i = 0; i < 1000; i++)
28     {
29         for (int j = 0; j < 1000; j++)
30         {
31             T value = vc[i * 1000 + j];
32         }
33     }
34     t = clock() - t;
35     cout << "'vector<"<<TOSTRING(T)<<">::operator[]' 1000000 times cost: " << t << endl;
36 }
37 int main()
38 {
39     printTime<int>();
40     printTime<bool>();
41     printTime<char>();    
42     return 0;
43 }
 
 

请教了qq群的大神们,介绍了一个叫做typeid的关键字..所以在c++专栏下<typeid是什么>的帖子中学习学习这个东西

 

转载于:https://www.cnblogs.com/silentNight/p/5499588.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值