last day (15 days later) » 

7:05 PM
Hi there!
Thanks a lot for taking some time to talk with me
 
No problem
 
I'm familiar with the variadic template solutions and the benefits of using them for this exact kind of problem
The question on macros is more for my own sadistic learning pleasure
 
Sure... just want to toss that out... since you're using templates anyway, it's not a bad solution
 
Also the codebase I used to work in used macros, and it's been killing me not being able to remember how they implemented it
 
Ah, well the prototype I was working on wasn't quite sadistic enough :)
 
7:06 PM
A template prototype?
 
Preprocessor prototype
 
Well if you were to post an answer with variadic templates, or with a PP macro I'd accept it in a heartbeat
Someone from the comments should put something up since you guys have been so helpful
 
I'm considering, for my own sadistics, a solution to not be one unless it also works with VS2012... which is the real pain
Hang on a sec...
 
ya it is
 
....working on Coliru...
 
7:11 PM
ah cool
 
So this is a simplified preprocessor metaprogram, trying to just boil down to essentials (in the hopes of finding a decent VS2012 port)...
...but the idea here is to genericise a bit what we're emitting... then you can write your entire function as a single macro, in terms of these support macros...
Your function body needs to expand for each case heterogeneously, and that's the real idea
 
That looks pretty cool!
 
Sometimes you want something like "int arg1, int arg2"; sometimes, "typename Arg1 arg1, typename Arg2 arg2", and so on
 
right
This is for counting macro argument count?
#define COUNT_I(_,_9,_8,_7,_6,_5,_4,_3,_2,X,...) X
#define PP_COUNT(...) COUNT_I(__VA_ARGS__,9,8,7,6,5,4,3,2,1,~)
 
You can do this by simply writing one macro to expand an argument to each form you want to expand into
Yeah, that's an argument counter
The problem with writing macros piecewise like this, however, is that you're dissecting your code...
It'll work, but it guts the semantics out and scatters it everywhere--and that makes it terribly difficult to use
 
7:15 PM
For VS you'll need one more expansion for it to work (iirc)
#define COUNT_I(_,_9,_8,_7,_6,_5,_4,_3,_2,X,...) X
#define COUNT_EXPAND( X ) COUNT_I( X )
#define PP_COUNT(...) COUNT_EXPAND( COUNT_I(__VA_ARGS__,9,8,7,6,5,4,3,2,1,~) )
 
Incidentally, the solution here miserably fails in VS2013's crappy preprocessor
(Which should be the same as 2012's crappy one... they haven't actually touched it in forever)
I think it's more difficult than that... you need an entirely different set of macros almost
 
Well I made this earlier and tested it out
#define COUNT_ARGS( ... ) COUNT_ARGS_EXPAND( (__VA_ARGS__, 5, 4, 3, 2, 1, 0) )
#define COUNT_ARGS_EXPAND( A ) COUNT_ARGS_IMPL A
#define COUNT_ARGS_IMPL( _1, _2, _3, _4, _5, N, ... ) N
 
Just getting the counter to work isn't bad
 
It works, but not for 0 arguments
 
...but every piece feeding into this doesn't work either
 
7:17 PM
ah
 
0 args is very unportable anyway
FOO() works just as well as a macro with one argument as zero; gnu has an extension for how to elide commas. VS also has an extension, and it works differently... the entire reason I'm putting -std=c++11 on the Coliru line despite only using the preprocessor here is because it ignores the extensions to the preprocessor
You can make a portable counter that counts 0's using a matcher technique--but then, with VS, you have to have a hack solution--I'm certainly not going to bother to shoot for that
So, yeah, probably in practice, I can beef up this type of solution and give you a sample, and it won't work for VS
 
haha yeah does not sound like it's worth the work
 
I'm not sure if I want to put in work trying to get VS to take it
 
I don't even think the codebase I used cared about the 0 argument case
 
For VS, you could use variadic templates--but if you do that, you may as well use it everywhere
Aside from this, you could simply dissect the code you're writing and take that approach--again, that will work, but will be hard to make readable
 
7:22 PM
It might be a good idea to let the user use the ## operator themself
I have an example, just a moment
 
Well we need to do n argument cases though
Ideally you'd want to take some definition, apply it to 1 arg, then 2 args, then 3 args, and so on; each has different numbers of parameters to expand into--and it's that pattern of expansion you want to abstract out
So in the raw PP_EAPPLY macro in this prototype--the parameter (1, 2, 3, 4, 5) would be the one that varies at the high level
 
ah yeah, I misunderstood a piece so my example didn't quite work
 
...so one more thing to show, just to sketch out the idea here: coliru.stacked-crooked.com/a/3950b64e6d6ec62b
You can see the MAKE_BAR_OP macro I have
That generates a very crummy imitation of the code you want to make... but it's abstracting out repetition
 
hmm that's very interesting
 
It's applying the PP_EAPPLY expander twice (names here are ugly but just for prototype); once to build the argument list, and the second time to build your assert calls
 
7:37 PM
it would take me some time to really read and understand it all though
I see
 
This is kind of a poor man's imitation of CHAOS_PP
But that's the idea here
 
I'll keep your example on hand to read more later today
Here's an example based on dyp's solution. The LOOP macro can be overloaded nicely to support more arguments than just 1 or 2
I have to run for now though. Thanks again for talking with me!
 
Yeah, I see--implementing specific types of features in--that works too
np... I'll just be picking at this solution in spare time FYI
 

last day (15 days later) »