Jump to content
  • Advertisement
Sign in to follow this  
cypherx

Passing void as an argument?

This topic is 4682 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]));
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!