Giter Club home page Giter Club logo

Comments (2)

thoni56 avatar thoni56 commented on July 22, 2024

This is a great idea!

I'm not familiar enough with GTK to see how they get away with this. Just calling a function/macro with more arguments than it has declared will surely fire error messages from most C compilers.

Or am I misunderstanding you? I would appreciate a more detailed sketch of how this would look.

from cgreen.

Kamil2000 avatar Kamil2000 commented on July 22, 2024

Ok, I will try to explain this whole C function pointers magic above:

The most important thing is to handle two
function signatures with a one macro. This can be done
with little bit of casting. Take a look at this trivial program:
#include <stdio.h>

typedef void (*Func2)(int, int);
typedef void (*Func1)(int);
void func(void) {
printf("LaLa\n");
}
int main()
{
Func2 func2 = (Func2)&func;
Func1 func1 = (Func1)&func;
func2(4,5);
func1(5);
return 0;
}

It will compile and run on any platform. However, this doesn't mean this
is a C-compilant program, because standard doesn't define
such calls at any point.

For more information please visit:
https://stackoverflow.com/questions/559581/casting-a-function-pointer-to-another-type
As one reply suggest, this is technically undefined behavior., but the second
one suggests that whole GNOME framework depends upon it, because of ABI
compatibility.

After some reconsideration I figured out we might use some other,
more standard C tricks to handle two types with a single macro.
In C11 there is a _Generic macro, which can be useful:
#include <stdio.h>

typedef void (*FuncA)(void);
typedef void (*FuncB)(int);

void funcA(void) {
printf("LaLa\n");
}
void funcB(int unused) {
(void)unused;
printf("TaTa\n");
}
void callA(FuncA a) {
a();
}
void callB(FuncB b) {
b(0);
}
#define CALL(x) _Generic(x, FuncA: callA, default: callB)(x)

int main()
{
CALL(funcA);
CALL(funcB);
return 0;
}

I personally like the second approach more sue to standard compliance
and cleaner error messages. If that approach is to be chosen sth like
with_parametrized_side_effects needs to be provided for for
older C versions.

Hope all is clear by now ;)

from cgreen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.