从一个模板函数聊聊模板函数里面如何获得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是什么>的帖子中学习学习这个东西

 

posted @ 2016-05-16 21:36  哈哈不是嘎嘎  阅读(350)  评论(0编辑  收藏  举报