Quote:Original post by okonomiyaki
What happens when you take out the template specializiation for void in _ArgParser?
It works perfectly. Except then you can't have functions which take no arguments, and is thus not really an acceptable solution :S
Quote:I'm not exactly sure, but it could have something to do with having templated functions within a templated class/struct. Never done that before but why not get rid of the _ArgParser class and have to global functions with two template arguments?
Short answer - because you can't partially specialize template functions, only templated classes. Hold on, lemmie post this then type out the explanation, because I feel I have to justify such craziness.
Okay, so basically, you have two cases - functions that return void and those that don't. If we look at the code I posted originally, you'll notice the line -
return _returnObject<R>( realfunc( a1 ) );
What this does is, essentially, cache the value of the function, then give those returned values back to python. Lets take a look at the code -
template < class R > PyObject* _returnObject( R val ) { PyObject* ret = Py_BuildValue( _getFmtString<R>().c_str(), val ); Py_DECREF( ret ); return ret;}
Seems okay, right?
Well, in most situations, yes. In fact, it works fine in every situation except the case when R = void. In this case, you're declaring a variable, var of type void, and passing it around and such. Which is completely and totally illegal.
Now, there's another problem, which is the Python syntax for returning void, which can be shown in the sister function -
inline PyObject* _returnNone() { Py_INCREF( Py_None ); return Py_None;}
Actually, I can't remember the exact reason that explicit specialization didn't work here, but I'm almost positive its because you can't define a function which takes a void parameter. Or something messy like that.
Anyway, from here you're basically required to have two separate instances of each parse function. Since we're only dealing with the return type parameter, we need partial specialization, which requires templated classes.
And eeeeeeek.
(Also, if you have a better solution for this part, I'm all ears :3)