how to use typeof of function to create another function with the same type

110 Views Asked by At

for example:

int f(int a, int b)
{
    return a + b;
}

typeof(f) f2
{
    return -f(a, b);
}

Is something like this possible in C?

Edit: The reason I'm asking this is I'm trying to do something like:

#define MAKE_NEGATION_OF(function) \
typeof(function) function##_negated { \
    return -function(a, b); \
}
3

There are 3 best solutions below

1
Oka On BEST ANSWER

I want use a macro to generate a function with the same type, but different return.

Note that your two examples (used to) differ (before the question was edited).

Given the previous typeof example, you could instead write a macro that generates functions with the same signature, differing only by key tokens:

#include <stdio.h>

#define DEFINE_BINARY_OPERATION(name, op) \
    int name(int a, int b) { \
        return a op b; \
    }

DEFINE_BINARY_OPERATION(add, +)
DEFINE_BINARY_OPERATION(subtract, -)

int main(void)
{
    printf("%d\n", add(5, 6));
    printf("%d\n", subtract(5, 6));
}
11
-1

Given the macro example, there is no need for another function, just negate the function call in a macro wrapper.

#include <stdio.h>

#define add_negated(...) (-add(__VA_ARGS__))

int add(int a, int b)
{
    return a + b;
}

int main(void)
{
    printf("%d\n", add(5, 6));
    printf("%d\n", add_negated(5, 6));
}
11
-11

If you need a function pointer, then this is an extremely rigid combination of both ideas, which does not scale well at all. If your actual use case is non-trivial, you should probably just write the functions and their signatures by hand.

#include <stdio.h>

#define DEFINE_BINARY_EXPRESSION(name, expr) \
    int name(int a, int b) { \
        return expr; \
    }
#define NEGATE_BINARY_EXPRESSION(name) \
    DEFINE_BINARY_EXPRESSION(name##_negated, -name(a, b))

DEFINE_BINARY_EXPRESSION(add, a + b)
NEGATE_BINARY_EXPRESSION(add)

DEFINE_BINARY_EXPRESSION(subtract, a - b)
NEGATE_BINARY_EXPRESSION(subtract)

int main(void)
{
    printf("%d\n", add(5, 6));
    printf("%d\n", add_negated(5, 6));

    printf("%d\n", subtract(5, 6));
    printf("%d\n", subtract_negated(5, 6));
}
11
-11
-1
1
0
dbush On

Functions cannot be created with a typedef or typeof expression, because such types do not contain any information regarding the names of parameters, and a function definition requires names for its parameters.

While you can declare a function with a typedef or typeof, you can't define one that way.

5
Farzam On

In C, you cannot directly create a new function with the same type as another function using a typeof construct or a similar mechanism like you can in some other languages.

However, you can achieve a relatively similar functionality by using function pointers and defining a new function that has the same signature (parameter types and return type) as an existing function. Here's an example:

#include <stdio.h>

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int main() {
    // Define a function pointer type with the same signature as 'add'
    typedef int (*BinaryIntFunction)(int, int);

    // Create a function pointer variable and assign 'add' to it
    BinaryIntFunction operation = add;

    // Use the function pointer to call 'add'
    int result = operation(5, 3);
    printf("Result: %d\n", result); // Output: Result: 8

    // Assign 'subtract' to the same function pointer variable
    operation = subtract;

    // Use the function pointer to call 'subtract'
    result = operation(5, 3);
    printf("Result: %d\n", result); // Output: Result: 2

    return 0;
}