Jump to content
  • Advertisement
Sign in to follow this  
pitchblack00

Did you know you can define functions like that in C?

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

Advertisement
This is something second-year students at my alma mater are expected to learn in depth if they hope to actually pass their third semester. The parsing rules for declarations in C are rather simple once you get them down.
 
Note of course that this is all much easier with typedefs and is by far the recommended way for a C programmer to do something like this:
 
typedef int(*IF)(void);

extern IF make_function(void);
 
In C++11 this all gets even easier using the new using syntax as you no longer have to remember where the name goes inside the typedef:
 
using IF = int(*)();

IF make_function();

Share this post


Link to post
Share on other sites

Same here. They had us do a lot of absurd declarations no sane person will ever need, just for practice. "Declare a function that returns an array of function pointers and takes a pointer to an array of int". Looking back, I think all they really wanted to see was who would be smart enough to break it down into multiple typedefs and who would be insane enough to do it all in one line.

Share this post


Link to post
Share on other sites

That's right, declarations I knew about as well, but this is definition. Anyway certainly not something I'd like to find in my code base, but that goes to my personal zoo of craziest C gibberish.

Share this post


Link to post
Share on other sites

I've typedefed function pointers before, but never a function type. What can one do with the type of a function?

 

 

The are used all the time in C++.  Many of the algorithmic functions take such a function. Functions that sort or search or generate or do something 'if' will all use them.

 

If you search through the C++ standard libraries you will find several hundred functions that take a predicate, binary predicate, unary function, binary function, or similar. 

 

These are just the same as the prototypes above, typedefs for functions of the form bool(*UnaryPredicate)(T), bool(*BinaryPredicate)(T,T), void(*UnaryFunction)(T), void(*BinaryFunction)(T,T) or similar. The typedef looks very similar to the ones in the original post, except they are templates.

 

The benefit is that you can say:  if(function(a,b)) and it just works.  An example is the std::for_each implementation:

template<class InputIterator, class Function>
Function for_each(InputIterator first, InputIterator last, UnaryFunction function)
{
   while (first!=last) {
      function (*first);
      ++first;
   }
   return fn; // or, since C++11: return move(fn);
}

In C++ just go through the <algorithm> header, you will find a large number of function overloads taking "Predicate" or "Function" signatures. You will find similar overloads in most of the container classes, and in many other locations through the libraries.

 

 

When people talk about using Lambda functions, this is precisely where a Lambda function is used.

Share this post


Link to post
Share on other sites


These are just the same as the prototypes above, typedefs for functions of the form bool(*UnaryPredicate)(T), bool(*BinaryPredicate)(T,T), void(*UnaryFunction)(T), void(*BinaryFunction)(T,T) or similar. The typedef looks very similar to the ones in the original post, except they are templates.

Those are function pointer typedefs, not function typedefs.

Share this post


Link to post
Share on other sites

 


These are just the same as the prototypes above, typedefs for functions of the form bool(*UnaryPredicate)(T), bool(*BinaryPredicate)(T,T), void(*UnaryFunction)(T), void(*BinaryFunction)(T,T) or similar. The typedef looks very similar to the ones in the original post, except they are templates.

Those are function pointer typedefs, not function typedefs.

 

 

you can make a pointer to a function type at least:

SomeFunctionType *foo;

 

not much sure of other uses.

would be cool if you could be like (in C):

SomeFunctionType Foo { ... }

or:

{

...

    Foo(SomeFunctionType { ... });   // make and pass closure here...

...

}

or something.

 

but, alas...

 

actually, it is sort of possible to do closures in plain C (without compiler extensions, and using plain function pointers), but tends to involve some amount of pain (and some bit of trickery in the background, and some restrictions), making it not really particularly viable as a general-purpose development practice.

Edited by BGB

Share this post


Link to post
Share on other sites


would be cool if you could be like (in C):
SomeFunctionType Foo { ... }

 

I think the reason why function types can't be used to define functions is because there would be no way to assign argument names, so they are pretty useless by themselves, you can't return them, you can't assign them, but you can construct other types with it.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!