Title: [C++] you can suck my cock, java! Author: Anonymous Pastebin link: http://pastebin.com/psUpijnV First Edit: Thursday 4th of July 2013 01:59:07 AM CDT Last Edit: Thursday 4th of July 2013 01:59:07 AM CDT //It is not easy understanding even most basic concepts of the new C++, that's true. For instance:   // First way of operating. template< bool B > struct Algorithm {     template static int do_it (T1 &, T2 &)  { /*some code here*/ } };   // Second way of operating. template<> struct Algorithm {     template static int do_it (T1, T2)  { /* different code here*/ } };   // Instantiating 'elaborate' will automatically instantiate the correct way to operate. template int elaborate (T1 A, T2 B) {     // Use the second way only if 'T1' is an integer and if 'T2' is a float     // otherwise use the first way.     return Algorithm::value && std::is_floating_point::value>::do_it( A, B ) ; }   /*why is it cool? first, elaborate is a template function. this means, that after declaring it just as so above, we can the write*/ int a,b; float c; double d; string S; elaborate(a,b); elaborate(a,c); elaborate(d,c); elaborate(S,S); //and all this is legal.   //a template function template swap(T& A, T& B) {     T C(A);  //create variable C of type T using value of A     A = B;   // assign value of B to A     B = C;   // assign value of C to B } /*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. There is a function in standard template library (STL), std::swap, that works this way, and is coded as above. Why use it? It basically saves you tons of writing.   Returning to previous example. so basically, at the end of the day, elaborate(a,b) will "run" the first way, elaborate(b,c) will "run" the second. why do we do it anyway? well, this way we have CONTROL over what happens. 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.   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.   for instance, if you substitute*/ //template with template::value>::type, class T2> //then the code will only compile, when T1 is an int. //This is somewhat silly example.   template auto reserve(T& t, size_t n) -> typename std::enable_if::type {   t.reserve(n);   return true; } template auto reserve(T& t, size_t n) -> typename std::enable_if::type {   std::cout<<"Die you lazy C scum\n";   return false; }   /*What does this baby do? we have a class foo1 that has foo1::reserve method, and foo2, which does not. we then write reserve(foo1, 10); reserve(foo2, 10); what does happen? the first instance checks that foo1 has reserve method, and then uses it. the second prints on screen, what does she think of you.   Why is is better than for instance try catch? Well, we can put ANY CODE WE WANT there. That's the power of metaprogramming. You have really insane control over what happens in your programme. Why is this important? Because when you write a library, for example, a game engine one, you want the end user to use it as EASILY as it is possible (given he has documentation of your work). He doesn't give a fuck about what EXACTLY happens, when he writes theBestLibrary::reserve(mobArray,10); but what the results are.*/