Victus Spiritus

home

Currying in c++

28 Apr 2011

Variable argument parameter lists

While brushing up on some coding samples I came across va_args which is included within the stdargs header file. Here's an example of how it's used:

To compile:
g++ va_args_test.cpp -o va_args 

it outputs:
>>Ten (doubles)10 twelve 12
>>Ten (longs)10 twelve 12

This triggered a mad idea and became part one of my many tiered master plan to mold c++ into my unwilling modern feeling, syntactically sugar coated servant (fallback). We'll see how far I get. Next up, currying! I leaned heavily on void* to make it work, and leveraged typedef to aid readability. Bear with me, the source is a little longer:

The code takes sCurry(nargs,function_pointer1,function_pointer2...,val) and computes it. Yeah I know boost has functors, good for that 80 trillion byte library ;).</p>

Here's an example of it run:

Mark-Essels-iMac:misc messel$ g++ curry.cpp -o curry
Mark-Essels-iMac:misc messel$ ./curry 2
Address to base numeric 0x7fff5fbfdbc8 base val 2
extracting val from curry 2
temp 2 val 1
temp 2 val 1
addOne-> 3 curried addOne 3
start curry val 3
extracting val from curry 3
temp 3 val 1
temp 4 val 2
temp 8 val 7
temp 1 val 10
curried output f10(f7(f2(f1(val)))) or ((val+1)*2-7)/10 = 0.1

Next up, dynamic typing without boost::any...maybe.

Notes:



<script type="text/javascript"
src="https://gist.github.com/raw/949945/1468755b2659aa0206ef4b0060100b152f44a8d3/growingdivs.js"></script>