-1

I just happened to stumble across the following question on stack overflow:

In gatomic.c of glib there are several function declarations that look like this:

gboolean
(g_atomic_int_compare_and_exchange_full) (gint *atomic,
                                          gint  oldval,
                                          gint  newval,
                                          gint *preval)
{
  return g_atomic_int_compare_and_exchange_full (atomic, oldval, newval, preval);
}

Where the OP of the question asks how this piece of code could work as it looks like a function that calls itself (it doesn't).

Why would anyone write code like this?

That is, defining a function with the exact same name as a macro.

Why not use two separate identifiers (or write the macro in ALL CAPS for that reason).

Possible answer: Why does the C library use macros and functions with same name? although this is about the C standard library, not some user library.

5
  • 2
    Does this answer your question? Why does the C library use macros and functions with same name? Jul 17, 2023 at 7:10
  • Did you even read the question? I linked that answer in my post.
    – Marco
    Jul 17, 2023 at 7:11
  • 3
    Yes, I did read the question in full and the reason for the duplicate vote is because in my opinion that answer applies here as well. Jul 17, 2023 at 7:12
  • Could you please edit your question to explain why you think there is a fundamental difference between the C stdlib and a user library? I agree with Bart that the reasons given there apply equally to a user library. Jul 17, 2023 at 7:26
  • Possibly one of the fundamental differences is that the C stdlib is not governed by the same restrictions as user code, e.g. identifiers starting with __ are reserved for the implementation, so its invalid to use them in user code. An argument could be made that this type of behaviour is acceptable for the C stdlib, but not user code.
    – Marco
    Jul 17, 2023 at 7:43

1 Answer 1

-3

Why not? It works, and it doesn’t affect anyone because it is hidden in a .c file. It’s a proven pattern, no need to change it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.