Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

you can suck my cock, java!

By: a guest on Jul 4th, 2013  |  syntax: C++  |  size: 3.77 KB  |  hits: 6  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //It is not easy understanding even most basic concepts of the new C++, that's true. For instance:
  2.  
  3. // First way of operating.
  4. template< bool B > struct Algorithm
  5. {
  6.     template<class T1, class T2> static int do_it (T1 &, T2 &)  { /*some code here*/ }
  7. };
  8.  
  9. // Second way of operating.
  10. template<> struct Algorithm<true> {
  11.     template<class T1, class T2> static int do_it (T1, T2)  { /* different code here*/ }
  12. };
  13.  
  14. // Instantiating 'elaborate' will automatically instantiate the correct way to operate.
  15. template<class T1, class T2>
  16. int elaborate (T1 A, T2 B)
  17. {
  18.     // Use the second way only if 'T1' is an integer and if 'T2' is a float
  19.     // otherwise use the first way.
  20.     return Algorithm<std::is_integral<T1>::value && std::is_floating_point<T2>::value>::do_it( A, B ) ;
  21. }
  22.  
  23. /*why is it cool?
  24. first, elaborate is a template function. this means, that after declaring it just as so above, we can the write*/
  25. int a,b;
  26. float c;
  27. double d;
  28. string S;
  29. elaborate(a,b); elaborate(a,c); elaborate(d,c); elaborate(S,S);
  30. //and all this is legal.
  31.  
  32. //a template function
  33. template<class T> swap(T& A, T& B)
  34. {
  35.     T C(A);  //create variable C of type T using value of A
  36.     A = B;   // assign value of B to A
  37.     B = C;   // assign value of C to B
  38. }
  39. /*will, well, swap every, EVERY single 2 variables of same type (being it int, string, your own class ect.) so long as this type has correct assignment operator and copy constructor.
  40. There is a function in standard template library (STL), std::swap, that works this way, and is coded as above.
  41. Why use it?
  42. It basically saves you tons of writing.
  43.  
  44. Returning to previous example.
  45. so basically, at the end of the day, elaborate(a,b) will "run" the first way, elaborate(b,c) will "run" the second.
  46. why do we do it anyway? well, this way we have CONTROL over what happens.
  47. most importantly, if we use only first way, the second way shouldn't even be included in the final programme (well, that depends on the compiler, but the language provides with such possibility), as all the checking is done at the time of compilation.
  48.  
  49. regarding elaborate(S,S), what will happen? well this is an example function. if strings (not talking about fucking char[] or char*, but masterrace std::string) are not something you would want to put there, there are numerous ways to make compiler stop you from doing so rather than the program itself failing at startup.
  50.  
  51. for instance, if you substitute*/
  52. //template<class T1, class T2> with
  53. template<class T1, class = typename std::enable_if<std::is_integral<T>::value>::type, class T2>
  54. //then the code will only compile, when T1 is an int.
  55. //This is somewhat silly example.
  56.  
  57. template <class T>
  58. auto reserve(T& t, size_t n) -> typename std::enable_if<has_reserve_method(t), bool>::type
  59. {
  60.   t.reserve(n);
  61.   return true;
  62. }
  63. template <class T>
  64. auto reserve(T& t, size_t n) -> typename std::enable_if<not has_reserve_method(t), bool>::type
  65. {
  66.   std::cout<<"Die you lazy C scum\n";
  67.   return false;
  68. }
  69.  
  70. /*What does this baby do?
  71. we have a class foo1 that has foo1::reserve method, and foo2, which does not.
  72. we then write
  73. reserve(foo1, 10);
  74. reserve(foo2, 10);
  75. what does happen? the first instance checks that foo1 has reserve method, and then uses it.
  76. the second prints on screen, what does she think of you.
  77.  
  78. Why is is better than for instance try catch?
  79. Well, we can put ANY CODE WE WANT there.
  80. That's the power of metaprogramming.
  81. You have really insane control over what happens in your programme.
  82. Why is this important?
  83. Because when you write a library, for example, a game engine one,
  84. you want the end user to use it as EASILY as it is possible (given he has documentation of your work).
  85. He doesn't give a fuck about what EXACTLY happens, when he writes theBestLibrary::reserve(mobArray,10);
  86. but what the results are.*/