Jump to content
  • Advertisement
Sign in to follow this  
cypherx

C++'s annoying limits... (is there something better?)

This topic is 4970 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

Does anyone know of extensions to C++ or a C-like language which will allow me to use nested functions, compose them, combine them with anonymous functions and and pass them to other functions? These features would save me a ton of time in writing my current game engine. I've looked at boost::lambda and fc++, but they are unbearably ugly hacks. (example of nested functions)
void main()
{
   int func1 (int x, int y)
   {
     return x+y; 
   }

   cout << endl << func1(3, 2)
}



(example of composed functions)

/* add this code to the main function above */ 
int func2 = func1(35); 

cout << endl << func2(62); //calls func1(35, 62)



(example of passing a function, combined with an anonymous function)
/* add this code to the main function above */ 

int List[3] = { 1, 2, 3 }

foreach(List, List+3, cout << func2(arg1) ); 
/* call func2(List) for i 0-2 */ 



Any ideas? Thanks, Alex

Share this post


Link to post
Share on other sites
Advertisement
D, Java, Lisp and Pascal support some or all of those features.
And don't forget that C++ supports nested functions through local class members, some compilers even have them as an extension (GCC).

Share this post


Link to post
Share on other sites
Lots of languages support closures and first-class functions. Some I'm more familiar with are LISP, Ruby, Python, LUA, or any 'functional' languages.

Share this post


Link to post
Share on other sites
Ananymous functions? i think a FUNCTION POINTER would take care of that easily, look it up, as for this "nested function" stuff... you want to add in functions into the middle of other functions at run time? that hardly seems useful or necessary, what are you trying to accomplish? i sincerely doubt there isnt an elegant solution

hope that helps
-Dan

Share this post


Link to post
Share on other sites
Quote:
Original post by Ademan555
i think a FUNCTION POINTER would take care of that easily


A function pointer does not carry context.

Quote:
that hardly seems useful or necessary, what are you trying to accomplish?


Closures are extremely powerful constructs.

Quote:
i sincerely doubt there isnt an elegant solution


In standard C++, there isn't really. He has already pointed out at the mainstream solutions (boost and FC++).

Share this post


Link to post
Share on other sites
Since you appear to be aware that anonymous functions and function composition/combinations are missing from C++, what languages let you use them? :) Can't you use that?

Share this post


Link to post
Share on other sites
I don't recommend the use of this, but it will do one of the things you want:
#define LOCAL_FUNCTION_START struct temp_local_struct { static 
#define LOCAL_FUNCTION_END };
#define LOCAL_FUNCTION_CALL temp_local_struct::

int main() {
LOCAL_FUNCTION_START
int func1 (int x, int y)
{
return x+y;
}
LOCAL_FUNCTION_END

cout << LOCAL_FUNCTION_CALL func1(3, 2) << endl;
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by cypherx
Does anyone know of extensions to C++ or a C-like language which will allow me to use nested functions, compose them, combine them with anonymous functions and and pass them to other functions? These features would save me a ton of time in writing my current game engine. I've looked at boost::lambda and fc++, but they are unbearably ugly hacks.
...
Any ideas?
I know it's not at all C like, but Haskell has probably the best support of these features of any language. Your examples:

-- Example of Nested Functions
main = do
putStr $ "\n" ++ func1 3 2
where func1 x y = x + y

-- Example of composed functions.
-- (Note that they're not technically composed, they're curried.)

main = do
putStr "\n"
putStr $ show $ func1 3 2
putStr "\n"
putStr $ show $ func2 62
where
func1 x y = x + y
func2 = func1 35

-- Example of passing a function, combined with an anonymous function

main = sequence $ map (print . func2) list
where list = [1,2,3]

-- Since I suppose the previous didn't really show a lambda expression,
-- I guess I'll use this example instead

main = sequence $ map (\x -> (print (func2 x))) list
where list = [1,2,3]


The only things that might be confusing are the application operator ($), which only exists to change the precedence, and the sequence function, which takes a sequence of IO actions (from the print function), and constructs a larger IO action (main) from sequencing them in order.

Python versions of these are even more straightforward:

def main():
def func1(x,y): return x+y
print '\n',func1(3,2),

def main():
def func1(x,y): return x+y
print '\n',func1(3,2),

func2 = lambda x: func1(35, x)
print '\n',func2(62),

import sys
def main():
list = [1,2,3]
map (lambda x: sys.stdout.write('\n' + `x`), list)


The only reason sys.stdout is necessary is because print statements are invalid inside a lambda, so I just used the standard output (which is where print always prints anyway, and is redirectable) file instead.

Share this post


Link to post
Share on other sites
ruby is not C like either, but i recommend a look at it anyway.

languages like C/C++ don't usually support closures because they are very expensive in non-interpretted langauges, lots of item copying, and possibly different behaviors anyway when it comes time to actually use the object (because of the eternal "no such thing as an infinitely deep copy")

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!