CppTemplateTutorial.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. #include "stdafx.h"
  2. #include <vector>
  3. #include <cstdint>
  4. #define WRONG_CODE_ENABLED 0
  5. // 0. Basic Form
  6. namespace _0
  7. {
  8. template <typename T> // Old fasion: template <class T>
  9. class ClassA
  10. {
  11. T a;
  12. T* b;
  13. T foo();
  14. void foo2(T const&);
  15. };
  16. template <int Sz>
  17. class ClassB
  18. {
  19. int arr[Sz];
  20. };
  21. size_t a = sizeof(ClassB<3>);
  22. size_t b = sizeof(ClassB<7>);
  23. template <typename T> void FunctionA(T const& param)
  24. {
  25. }
  26. template <typename T> T FunctionB()
  27. {
  28. return T();
  29. }
  30. }
  31. // 1.1 Nested in Class
  32. namespace _1_1
  33. {
  34. template <typename T> // Old fasion: template <class T>
  35. class ClassA
  36. {
  37. T a;
  38. T* b;
  39. T foo();
  40. template <typename U> void foo2(T const&, U const&);
  41. };
  42. }
  43. // 1.2 Instanciating 1
  44. namespace _1_2
  45. {
  46. _1_1::ClassA<int> a;
  47. #if WRONG_CODE_ENABLED
  48. _1_1::ClassA<WhatTheFuck> b; // Wrong
  49. _1_1::ClassA c; // Wrong
  50. #endif
  51. }
  52. // 1.2.2
  53. namespace _1_2_2
  54. {
  55. template <typename T> T Add(T a, T b)
  56. {
  57. return a + b;
  58. }
  59. template <typename SrcT, typename DstT> DstT c_style_cast(SrcT v)
  60. {
  61. return (DstT)(v);
  62. }
  63. #if WRONG_CODE_ENABLED
  64. void foo()
  65. {
  66. int a = 0;
  67. int b = 0;
  68. char c = 0;
  69. Add(b, c);
  70. }
  71. void foo2()
  72. {
  73. int v = 0;
  74. float i = c_style_cast<float>(v);
  75. }
  76. #endif
  77. }
  78. // 1.3 Instanciating 2
  79. namespace _1_3
  80. {
  81. template <int i> class A
  82. {
  83. public:
  84. void foo()
  85. {
  86. }
  87. };
  88. template <uint8_t a, typename b, void* c> class B {};
  89. template <void (*a)()> class C {};
  90. template <void (A<3>::*a)()> class D {};
  91. #if WRONG_CODE_ENABLED
  92. template <float a> class E {};
  93. #endif
  94. void foo()
  95. {
  96. A<5> a;
  97. B<7, A<5>, nullptr> b;
  98. C<&foo> c;
  99. D<&A<3>::foo> d;
  100. #if WRONG_CODE_ENABLED
  101. int x = 3;
  102. A<x> b;
  103. #endif
  104. }
  105. #if WRONG_CODE_ENABLED
  106. const char* s = "abc";
  107. template <char const* s> class S
  108. {
  109. };
  110. void foo2()
  111. {
  112. S<"abc"> i;
  113. }
  114. #endif
  115. template <typename T>
  116. class ClassB
  117. {
  118. T* a;
  119. };
  120. template <typename T>
  121. class ClassC
  122. {
  123. T a;
  124. };
  125. struct StructA; // Declared but not be defined
  126. ClassB<StructA> d; // Right
  127. #if WRONG_CODE_ENABLED
  128. ClassC<StructA> e; // Wrong
  129. #endif
  130. }
  131. namespace _2_2_2
  132. {
  133. template <typename T> class AddFloatOrMulInt
  134. {
  135. static T Do(T a, T b)
  136. {
  137. // 在这个例子里面一般形式里面是什么内容不重要,因为用不上
  138. // 这里就随便给个0吧。
  139. return T(0);
  140. }
  141. };
  142. // 其次,我们要指定T是int时候的代码,这就是特化:
  143. template <> class AddFloatOrMulInt<int>
  144. {
  145. public:
  146. static int Do(int a, int b)
  147. {
  148. return a * b;
  149. }
  150. };
  151. // 再次,我们要指定T是float时候的代码:
  152. template <> class AddFloatOrMulInt<float>
  153. {
  154. public:
  155. static float Do(float a, float b)
  156. {
  157. return a * b;
  158. }
  159. };
  160. void foo()
  161. {
  162. float a(0), b(1);
  163. float c = AddFloatOrMulInt<float>::Do(a, b);
  164. }
  165. }
  166. namespace _2_2_3
  167. {
  168. template <typename T> class TypeToID
  169. {
  170. public:
  171. static int const ID = -1;
  172. };
  173. class B {};
  174. template <> class TypeToID<void ()>; // 函数的TypeID
  175. template <> class TypeToID<int[3]>; // 数组的TypeID
  176. template <> class TypeToID<int (int[3])>; // 这是以数组为参数的函数的TypeID
  177. template <> class TypeToID<int (B::*[3])(void*, float[2])>; // 我也不知道这是什么了,自己看着办吧。
  178. template <> class TypeToID<int const * volatile * const volatile>;
  179. }
  180. namespace _2_2_4
  181. {
  182. template <typename T> struct X {};
  183. template <typename T> struct Y
  184. {
  185. typedef X<T> ReboundType;
  186. typedef typename X<T>::MemberType MemberType;
  187. #if WRONG_CODE_ENABLED
  188. typedef WTF MemberType3;
  189. #endif
  190. static void foo()
  191. {
  192. X<T> instance0;
  193. X<T>::MemberType instan
  194. WTF instance2
  195. 大王叫我来巡山 - + &
  196. }
  197. };
  198. void foo()
  199. {
  200. #if WRONG_CODE_ENABLED
  201. Y<int>::foo();
  202. #endif
  203. }
  204. }
  205. // 1.4 Specialization, Partial Specialization, Full Specialization
  206. namespace _1_4
  207. {
  208. // Prototype of Templates I: Single Parameter
  209. template <typename T> class ClassD
  210. {
  211. int a;
  212. };
  213. // Specialization: Write a pattern for matching
  214. template <> class ClassD<int> // 1. template <> 2. ClassD<int>
  215. {
  216. int b;
  217. };
  218. template <> class ClassD<float>
  219. {
  220. int c;
  221. };
  222. // Partial-Specialization: A partial pattern for matching
  223. template <typename T> class ClassD<T*> // 1. template <typename T> 2. ClassD<T*>
  224. {
  225. int d;
  226. };
  227. template <> class ClassD<int*> // 1. template <> 2. ClassD<T*>
  228. {
  229. int e;
  230. };
  231. // Question:
  232. // ClassD<int>::?
  233. // ClassD<float>::?
  234. // ClassD<double>::?
  235. // ClassD<double*>::?
  236. // ClassD<int*>::?
  237. // ClassD<int const*>::?
  238. // Prototype of Templates II: Multiple Parameter
  239. template <typename T, typename U> class ClassE
  240. {
  241. int a;
  242. };
  243. template <typename T, typename U> class ClassE<T, U*>
  244. {
  245. int b;
  246. };
  247. template <typename T> class ClassE<T, int>
  248. {
  249. int c;
  250. };
  251. template <typename T> class ClassE<T, int*>
  252. {
  253. int d;
  254. };
  255. template <typename U> class ClassE<int, U>
  256. {
  257. int e;
  258. };
  259. template <> class ClassE<int, int>
  260. {
  261. int f;
  262. };
  263. // Question:
  264. // ClassE<float, double>::?
  265. // ClassE<float, int>::?
  266. // ClassE<int, float>::?
  267. // ClassE<int, int*>::?
  268. // ClassE<int, int>::?
  269. // Member function specialization
  270. template <typename T>
  271. class ClassF
  272. {
  273. public:
  274. void foo();
  275. };
  276. template <typename T>
  277. void ClassF<T>::foo()
  278. {
  279. }
  280. template <>
  281. void ClassF<int>::foo()
  282. {
  283. }
  284. void foo()
  285. {
  286. ClassF<int>().foo();
  287. ClassF<float>().foo();
  288. }
  289. }
  290. // 2.1 Function Specialization
  291. namespace _2_1
  292. {
  293. // Overload is enabled but no partial-specialization
  294. template <typename T> void foo(T const& x) {}
  295. template <typename T> void foo(T& y) {}
  296. void foo(int&) {}
  297. void foo(int) {}
  298. // Specialization or Overloading
  299. template <> void foo<bool>(bool const& x) {}
  300. // Overloading
  301. template <typename T> void foo(T const*) {}
  302. template <typename T, typename U> void foo2(T const&, U const&);
  303. #if WRONG_CODE_ENABLED
  304. template <typename U> void foo2<int, U>(int const&, U const&);
  305. template <typename T, typename U> void foo2<T, U>(int const&, U const&);
  306. #endif
  307. // Overloading - Looks like partial specification
  308. template <typename U> void foo2(int const&, U const&);
  309. template <typename T, typename U> void foo2(T const*, U const&);
  310. // Don't forgot
  311. // T foo(...);
  312. // Specialize types which cannot be inferred by parameter
  313. template <typename UninferableT, typename InferableT>
  314. UninferableT foo3(InferableT const&) { return UninferableT(); }
  315. void test()
  316. {
  317. int x = 5;
  318. float y = 10.0f;
  319. foo(y);
  320. int const z = 5;
  321. foo(z);
  322. foo(true);
  323. foo3<int>(0.0f); // Specialize types which is uninferable.
  324. #if WRONG_CODE_ENABLED
  325. foo(3); // Ambigous
  326. foo(x); // Ambigous
  327. #endif
  328. }
  329. }
  330. // 2.2 Example: Derived from template.
  331. namespace _2_2
  332. {
  333. template <typename T>
  334. class ClassA
  335. {
  336. T x;
  337. };
  338. template <typename T>
  339. class ClassB
  340. {
  341. T* x;
  342. };
  343. template <typename T>
  344. class ClassC: public ClassB<T>
  345. {
  346. T* x;
  347. };
  348. ClassC<int> a;
  349. #if WRONG_CODE_ENABLED
  350. class ClassC: public ClassA<ClassC>
  351. {
  352. };
  353. #endif
  354. class ClassD: public ClassB<ClassD>
  355. {
  356. };
  357. // ClassC =??= ClassD
  358. }
  359. // 3.1 Meta Switch-Case/If-Then-Else via Specialization
  360. namespace _3_1
  361. {
  362. bool equal(int a, int b)
  363. {
  364. return a == b;
  365. }
  366. // meta functions:
  367. // bool equal0(TypeA, TypeB)
  368. // {
  369. // return false;
  370. // }
  371. // bool equal1(TypeA, TypeA)
  372. // {
  373. // return true;
  374. // }
  375. // equal(A, A) == equal1(A, A) == true
  376. // euqla(A, B) == equal0(A, B) == false
  377. template <typename T, typename U>
  378. class Equal
  379. {
  380. public:
  381. static bool const value = false;
  382. };
  383. template <typename T>
  384. class Equal<T, T>
  385. {
  386. public:
  387. static bool const value = true;
  388. };
  389. bool x = Equal<int, float>::value;
  390. bool y = Equal<int, int>::value;
  391. }
  392. // 3.2 SFINAE: Substitution Failure Is Not An Error.
  393. namespace _3_2
  394. {
  395. class ClassA
  396. {
  397. };
  398. template <int Sz> struct Mark
  399. {
  400. char _[Sz];
  401. };
  402. #if WRONG_CODE_ENABLED
  403. template <typename T>
  404. Mark<1> TestIncrementAdd(T const& v)
  405. {
  406. T tmp = v;
  407. ++tmp;
  408. return Mark<1>();
  409. }
  410. template <typename T>
  411. Mark<2> TestIncrementAdd(T const& v)
  412. {
  413. return Mark<2>();
  414. }
  415. bool a = TestIncrementAdd( ClassA() ) ) == sizeof(Mark<1>);
  416. #endif
  417. // Right case: From Wiki
  418. class ClassB
  419. {
  420. public:
  421. typedef int Marker;
  422. };
  423. template <typename T> void test(typename T::Marker) { }
  424. template <typename T> void test(T) { }
  425. void DoTest()
  426. {
  427. test<ClassB>(10); // Call #1.
  428. test<int>(10); // Call #2. SFINAE for test(T::Marker).
  429. }
  430. }
  431. // 3.3 Application: Type Traits
  432. namespace _3_3
  433. {
  434. template <typename T, typename U> class is_same;
  435. template <typename B, typename D> class is_base_of;
  436. // is_base_of
  437. // 1. B is class, D is also class.
  438. // 2. D* could be convert to B*
  439. // 3. B != D
  440. // Fundamentals
  441. typedef char Accepted;
  442. typedef int Rejected;
  443. class B
  444. {
  445. };
  446. class D: public B
  447. {
  448. };
  449. class D2: public D
  450. {
  451. };
  452. // Type is a class
  453. template <typename T>
  454. class is_class
  455. {
  456. private:
  457. // SFINAE
  458. template <typename U> static Accepted test( int U::* );
  459. template <typename U> static Rejected test(...);
  460. public:
  461. static const bool value = sizeof( test<T>(0) ) == sizeof(Accepted);
  462. };
  463. bool a = is_class<int>::value;
  464. bool b = is_class<B>::value;
  465. // B* could be convert to D*
  466. template <typename Source, typename Dest>
  467. class Convertible
  468. {
  469. private:
  470. // Not SFINAE
  471. static Accepted test(Dest*);
  472. static Rejected test(...);
  473. public:
  474. static const bool value = sizeof( test(static_cast<Source*>(NULL)) ) == sizeof(Accepted);
  475. };
  476. bool c = Convertible<B, D>::value;
  477. bool d = Convertible<D, B>::value;
  478. bool e = Convertible<B, int>::value;
  479. // B != D
  480. using _3_1::Equal;
  481. template <typename Base, typename Derived>
  482. class is_base_of
  483. {
  484. public:
  485. static bool const value =
  486. is_class<Base>::value &&
  487. is_class<Derived>::value &&
  488. Convertible<Base, Derived>::value &&
  489. !Equal<Base, Derived>::value;
  490. };
  491. bool f = is_base_of<B, D2>::value;
  492. bool g = is_base_of<D2, D>::value;
  493. bool h = is_base_of<B, int>::value;
  494. bool i = is_base_of<float, int>::value;
  495. // Questions:
  496. // remove_reference
  497. // remove_pointer
  498. // remove all qualifiers
  499. }
  500. // 3.4 Application: "Recursive" and Meta-Programming
  501. namespace _3_4
  502. {
  503. // sum a, a+1, ..., b-1, b
  504. int basic_algo(int a, int b)
  505. {
  506. int result = 0;
  507. for (int i = a; i <= b; ++i)
  508. {
  509. result += i;
  510. }
  511. return result;
  512. }
  513. // Template could not support variable
  514. // sum [a, b] without variable
  515. int recursive_algo(int a, int b)
  516. {
  517. if (a == b)
  518. {
  519. return b;
  520. }
  521. return a + recursive_algo(a+1, b);
  522. }
  523. // Translate to meta-programming
  524. template <int a, int b>
  525. class MetaSum
  526. {
  527. public:
  528. static int const value = MetaSum<a+1, b>::value + a;
  529. };
  530. template <int a>
  531. class MetaSum<a, a>
  532. {
  533. public:
  534. static int const value = a;
  535. };
  536. int a = MetaSum<1, 10>::value;
  537. }
  538. // 3.5 Application: Meta-Fibonacci
  539. namespace _3_5
  540. {
  541. template <int Index>
  542. class Fibonacci
  543. {
  544. public:
  545. static int const value = Fibonacci<Index - 1>::value + Fibonacci<Index - 2>::value;
  546. };
  547. template <>
  548. class Fibonacci<0>
  549. {
  550. public:
  551. static int const value = 0;
  552. };
  553. template <>
  554. class Fibonacci<1>
  555. {
  556. public:
  557. static int const value = 1;
  558. };
  559. int a = Fibonacci<8>::value;
  560. }
  561. // 4 Directive word: typename and template
  562. namespace _4
  563. {
  564. // typename T::type x;
  565. // ??? typename ???
  566. // typename T::template U<type> x;
  567. // ??? template ???
  568. class ClassA
  569. {
  570. public:
  571. typedef int NestedType;
  572. };
  573. class ClassB
  574. {
  575. public:
  576. typedef ClassA::NestedType NestedType;
  577. };
  578. template <typename T>
  579. class ClassC
  580. {
  581. public:
  582. #if WRONG_CODE_ENABLED
  583. typedef T::NestedType NestedType;
  584. #endif
  585. typedef typename T::NestedType NestedType;
  586. typedef typename std::vector<T>::iterator iterator;
  587. };
  588. class ClassD
  589. {
  590. public:
  591. template <typename U, typename V> class NestedType;
  592. };
  593. template <typename T>
  594. class ClassE
  595. {
  596. public:
  597. template <typename U> class NestedType;
  598. };
  599. template <typename T, typename U>
  600. class ClassF
  601. {
  602. #if WRONG_CODE_ENABLED
  603. typedef typename T::NestedType<U> NestedType;
  604. #endif
  605. typedef typename T::template NestedType<U, int> NestedType;
  606. typedef typename ClassE<T>::template NestedType<U> NestedType2;
  607. };
  608. ClassC<ClassB> a;
  609. ClassF<ClassD, float> b;
  610. }
  611. // 5.1 How to Construct Meta Operators
  612. namespace _5_1
  613. {
  614. // Expression = Value/Data Structure + Operator/Operations
  615. // Value in Templates:
  616. // Integral Constant (bool, char, unsigned, ...)
  617. // Type (typename)
  618. // 1. Trick: Constant <--> Type
  619. template <int i>
  620. class int_
  621. {
  622. public:
  623. static int const value = i;
  624. };
  625. int a = int_<5>::value;
  626. // This trick could work with overloading
  627. template <typename T>
  628. void Do(T* obj, int_<2>)
  629. {
  630. }
  631. template <typename T>
  632. void Do(T* obj, int_<1>)
  633. {
  634. }
  635. void foo()
  636. {
  637. Do( static_cast<int*>(nullptr), int_<1>() );
  638. }
  639. template <typename T, int i> void DoAnotherWay(T* obj)
  640. {
  641. }
  642. // Boolean is more useful than integral in general.
  643. template <bool v>
  644. class bool_
  645. {
  646. public:
  647. static bool const value = v;
  648. };
  649. typedef bool_<true> true_;
  650. typedef bool_<false> false_;
  651. #if WRONG_CODE_ENABLED
  652. // Aha, function cannot support partial specialization.
  653. template <typename T> void DoAnotherWay<T, 1>(T* obj) {}
  654. template <typename T> void DoAnotherWay<T, 2>(T* obj) {}
  655. #endif
  656. // 2. Operators:
  657. // add
  658. template <typename T, typename U>
  659. class add_
  660. {
  661. public:
  662. typedef int_<T::value + U::value> type;
  663. static int const value = type::value;
  664. };
  665. #if WRONG_CODE_ENABLED
  666. // conflict
  667. template <int x, int y>
  668. class add_
  669. {
  670. public:
  671. typedef int_<x+y> type;
  672. static int const value = type::value;
  673. };
  674. #endif
  675. template <int x, int y>
  676. class add_c
  677. {
  678. public:
  679. typedef int_<x+y> type;
  680. static int const value = type::value;
  681. };
  682. typedef add_< int_<2>, int_<3> >::type sum;
  683. int b = sum::value;
  684. typedef add_< int_<2>, int_<3> >::type sum_c;
  685. int c = sum_c::value;
  686. // another solution
  687. template <typename T, typename U>
  688. class add2_: public int_<T::value+U::value>
  689. {
  690. };
  691. int d = add2_< int_<2>, int_<3> >::value;
  692. // Other operators: sub, not, or, and ...
  693. }
  694. // 5.2 Example of Meta Programming: Meta-Vector
  695. namespace _5_2
  696. {
  697. // Array: elem[count]
  698. // Meta Array ?
  699. // Recursively Definition
  700. // 'Null' terminated
  701. template <typename HeadT, typename TailT>
  702. class pair_
  703. {
  704. typedef HeadT head;
  705. typedef TailT tail;
  706. };
  707. class Nil;
  708. // Try Use It to Definition
  709. typedef pair_< int, pair_<float, pair_<double, Nil> > > vector_3;
  710. template <typename T0, typename T1 = Nil, typename T2 = Nil, typename T3 = Nil>
  711. class make_vector_
  712. {
  713. typedef pair_< T0, make_vector_<T1, T2, T3> > type;
  714. };
  715. template <>
  716. class make_vector_<Nil, Nil, Nil, Nil>
  717. {
  718. typedef Nil type;
  719. };
  720. template <typename T0, typename T1 = Nil, typename T2 = Nil, typename T3 = Nil>
  721. class vector_: public make_vector_<T0, T1, T2, T3>::type
  722. {
  723. };
  724. typedef vector_<double, float, int> vector3;
  725. // Let's meta-program further
  726. //
  727. // push_back ? tip: push_back<Vector, Element>::type
  728. // pop ?
  729. // find ?
  730. // size ?
  731. }
  732. // 6.1 Template-Template Class
  733. // 6.2 High order function, closure and STL allocator rebind
  734. int _tmain(int argc, _TCHAR* argv[])
  735. {
  736. return 0;
  737. }