Custom C++ Preprocessor / Typeful Macros -


Given the benefits of metaprogramming in Ruby and Python, but lower-level language like C- + and C I'm thinking that for an actual work, an example for combining both comes in simple problems to sort out the list of arbitrary structures / classes, for example:

  stratus {int a; Int b; }; The vector & lt; S & gt; Vec; (Int x = 0; x & lt; 10; x ++); Inst.a = x; Inst.b = x + 10; Vec.push_back (inst); }  

Eventually, I want to be able to sort my voluntary sort with minimum minimum boilerplate code. The easiest way I can do this is to use STL type:

  sort (vec.begin (), vec.end ());  

However, for this I have to write a method that can compare "strats s" to what I want to do:

  sort (vec , ASC, B.Desc);  

Which is not very clearly valid C ++

What is the best way to fulfill my dream? If I had some types of macros, which would reveal to me how the elements of a vector were, then it would be trivial to write the C preprocessor macros to make the necessary functions to sort it.

It seems optional to write my own preprocessor, it works well, as long as I have to remove the type of "vec" is an easy way to do this?

Reference: Less Code = Less Bugs, Programming Competitions.

For the top, you can use your compose function to write inline, such as Python Lambda :

  Promotion of namespace: using lambda; Std :: sort (vec.begin (), vec.end (), (_1 - & gt; * & amp; A :: A) & lt; (_2 - ​​& gt; * & amp; s :: a));  

It surely assumes that you are sorting by A.

If the expressions you are seeing are more complex, you are better to write a different function; Even with basic support for ports, even in languages ​​such as Python and Ruby, despite the complex closure, it is not quite unreadable.

Warning: The above code has not been tested.

Hope it helps!


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -