Passing void as an argument?

Started by
4 comments, last by cypherx 18 years, 3 months ago
Hi, I'm trying to overload a function by the return type of another function. I can't quite figure out what to do with void types, however.

ReturnType convert(float data) { return new Number(data); }
ReturnType convert(void) { return Nil; }

void f() {...}

ReturnType r = convert( f() ); //fails

Any ideas? -Alex
Advertisement
Cannot do. If you try to use a void as an argument, you'll get a syntax error.

void a() { ... }...{    b(a()); // ERROR regardless of what b's signature is}

There's a tiny exception to this rule relating to templates, but it's not useful to you. What are you trying to do with this, anyway?
You can't do that, find some other way to express your functionality. If you give a little more information I'm sure someone can point you to a good solution.
Thanks for the replies. This problem came up while writing a C++ interface for my lisp interpreter.
Every C++ function is bound by an ExternalFunction object. ExternalFunction::eval(LispState& state, Expression arguments) converts the argument list to from lisp data to C++ types, calls the bound function with converted args, and then converts the C++ function's return value to lisp data.

It's that last step that's giving me trouble. What do I do when the C++ function returns void?

Thus far I've specialized the ExternalFunction classes for void return types...but this causes a lot of code duplication (which is problematic, since I have a specialization of ExternalFunction for every possible number arguments...lots and lots of code).

So do I have any way to automatically convert the return value of a void function? Or should I go on specializing for the void return type?

Thanks,
Alex
Perhaps something like this?
Atom invoke(void(*func)()) {  func();  return Atom::NIL;}template<typename r>Atom invoke(r(*func)()) {  return make_atom(func());}template<typename p0>Atom invoke(void(*func)(p0), const p0& arg0) {  func(arg0);  return Atom::NIL;}template<typename r, typename p0>Atom invoke(r(*func)(p0), const p0& arg0) {  return make_atom(func(arg0));}// Extend as needed for functions taking more arguments. Then:Atom ExternalFunction::eval(LispState& state, Expression arguments) {  // ...  if (add_x_y) {    return invoke(std::plus<int, int>, lisp2cpp_cast<int>(arguments[1]),                                       lisp2cpp_cast<int>(arguments[2]));  }}
Zahlman,

That's twice now that you've helped me clear mental cobwebs, thanks a lot.

This topic is closed to new replies.

Advertisement