c++11 template 模板练习

直接上代码吧

to do 

  1 // 111111.cpp: 定义控制台应用程序的入口点。
  2 //
  3 
  4 #include "stdafx.h"
  5 #include <iostream>
  6 #include <map>
  7 #include <sstream>
  8 #include <algorithm>
  9 #include <vector>
 10 #include <tuple>
 11 #include <type_traits>
 12 
 13 using namespace std;
 14 
 15 void sample1()
 16 {
 17     //基本的type trait
 18     int i = 3;
 19 
 20     if (true == std::is_void<int>::value) {
 21         std::cout << true << std::endl;
 22     }
 23     else {
 24         std::cout << false << std::endl;
 25     }
 26 
 27     if (true == std::is_const<int>::value) {
 28         std::cout << true << std::endl;
 29     }
 30     else {
 31         std::cout << false << std::endl;
 32     }
 33 
 34     if (true == std::is_const<int* const>::value) {
 35         std::cout << true << std::endl;
 36     }
 37     else {
 38         std::cout << false << std::endl;
 39     }
 40 
 41     if (true == std::is_same<int, signed int>::value) {
 42         std::cout << true << std::endl;
 43     }
 44     else {
 45         std::cout << false << std::endl;
 46     }
 47 }
 48 
 49 void sample2()
 50 {
 51     //从 remove_cv remove_reference 到 decay
 52     // const volatile int(remove_cv)--> int == signed int
 53     if (true == std::is_same< std::remove_cv<const volatile int>::type, signed int>::value) {
 54         std::cout << true << std::endl;
 55     }
 56     else {
 57         std::cout << false << std::endl;
 58     }
 59 
 60     // const volatile int&(remove_reference remove_cv )--> int == signed int
 61     // equal std::decay
 62     if (true == std::is_same< std::remove_cv< std::remove_reference< const volatile int&>::type>::type, signed int>::value) {
 63         std::cout << true << std::endl;
 64     }
 65     else {
 66         std::cout << false << std::endl;
 67     }
 68 
 69 
 70     // const volatile int&(remove_reference remove_cv )--> int == signed int
 71     // equal std::decay<>
 72     if (true == std::is_same< std::decay< const volatile int&>::type, signed int>::value) {
 73         std::cout << true << std::endl;
 74     }
 75     else {
 76         std::cout << false << std::endl;
 77     }
 78 }
 79 //===============================================================
 80 // std::decay 还可以用于将函数退化成函数指针 保存 用于延时执行
 81 
 82 template<typename F>
 83 struct SimpleFunction {
 84     using FnType = typename std::decay<F>::type;
 85     SimpleFunction(F f) :m_fn(f) {}
 86     void run() { m_fn(); }
 87     FnType m_fn;
 88 };
 89 
 90 void SimpFunctiontest() {
 91     std::cout << __FUNCTION__ << std::endl;
 92 }
 93 
 94 void sample3()
 95 {
 96     SimpleFunction<void()> f(SimpFunctiontest);
 97     f.run();
 98 }
 99 //===================================================
100 template<typename T>
101 typename std::enable_if<std::is_same<T, int>::value, T>::type
102 foo(T t) {
103     return t;
104 }
105 
106 void sample4()
107 {
108     //enable_if
109     int i = 1;
110     foo(i);  //编译通过
111     char b = 'x';
112     //foo(b) //编译无法通过 因为 b的type不是int
113 }
114 //==============================================================
115 class Person {
116 public:
117     Person(std::string s) :name(s) {}
118     std::string name;
119 };
120 
121 std::string funtion(Person p) {
122     return p.name;
123 }
124 
125 
126 template<typename Fn>
127 multimap<typename std::result_of<Fn(Person)>::type,Person>
128 GroupBy(const vector<Person>& vt, Fn&& keySelector)
129 {
130     typedef typename std::result_of<Fn(Person)>::type key_type;
131     multimap<key_type, Person> map;
132     std::for_each(vt.begin(), vt.end(), [&](const Person& person) 
133     {
134         map.insert(make_pair(keySelector(person), person));
135     });
136     return map;
137 }
138 
139 void sample5() {
140     // result_of 获取函数返回值类型
141     vector<Person> vp;
142     vp.push_back(Person("1"));
143     vp.push_back(Person("2"));
144     vp.push_back(Person("3"));
145      GroupBy(vp, funtion);
146 }
147 //==============================================================
148 template<typename T>
149 typename std::enable_if<std::is_arithmetic<T>::value,int>::type foo1(T t)
150 {
151     std::cout << t << std::endl;
152     return 0;
153 }
154 
155 template<typename T>
156 typename std::enable_if<!std::is_arithmetic<T>::value, int>::type foo1(T t)
157 {
158     std::cout << typeid(T).name() << std::endl;
159     return 1;
160 }
161 
162 void sample6()
163 {
164     //enable_if
165     int i = 1;
166     std::string b = "123ffdh";
167     foo1(i);
168     foo1(b);
169 }
170 //==========================================================================================
171 template<typename T>
172 typename std::enable_if<std::is_arithmetic<T>::value,std::string>::type
173 MyToString(T& t) { return std::to_string(t); }
174 
175 template<typename T>
176 typename std::enable_if<std::is_same<T,std::string>::value,std::string>::type
177 MyToString(T& t) { return t; }
178 
179 void sample7()
180 {
181     //enable_if示例
182     int i = 1;
183     MyToString(i);
184     double d = 123.213;
185     MyToString(d);
186     std::string s = "zxd12";
187     MyToString(s);
188 }
189 //===============================================================
190 template<std::size_t I =0,typename Tuple>
191 typename std::enable_if<I == std::tuple_size<Tuple>::value>::type printtp(std::string s,const Tuple& t){
192     std::cout << std::endl <<s << std::endl;
193 }
194 
195 template<std::size_t I = 0, typename Tuple>
196 typename std::enable_if<I < std::tuple_size<Tuple>::value>::type printtp(std::string& s,const Tuple& t) {
197     std::cout << std::get<I>(t) << std::endl;
198     s += std::to_string(I);
199     s += " ";
200     printtp<I + 1>(s,t);
201 }
202 
203 template<typename ... Args>
204 void MyPinrt(Args... args)
205 {
206     std::string s;
207     printtp(s,std::make_tuple(args...));
208 }
209 
210 void sample8()
211 {
212     MyPinrt(1,1.2324,false,"safda");
213 }
214 
215 //===========================================================================
216 int main()
217 {
218     /*sample1();
219     sample2();
220     sample3();
221     sample4();*/
222     //sample5();
223     //sample6();
224     sample7();
225     sample8();
226     return 0;
227 }
View Code

 

posted on 2018-01-05 15:15  itdef  阅读(803)  评论(0编辑  收藏  举报

导航