-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.

asked Jul 17, 2023 at 4:23
5
  • 2
    Does this answer your question? Why does the C library use macros and functions with same name? Commented Jul 17, 2023 at 7:10
  • Did you even read the question? I linked that answer in my post. Commented 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. Commented 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. Commented 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. Commented 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.

answered Jul 17, 2023 at 5:14

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.