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

Marco
  • 367
  • 1
  • 9
  • 2
    Does this answer your question? [Why does the C library use macros and functions with same name?](https://softwareengineering.stackexchange.com/questions/159846/why-does-the-c-library-use-macros-and-functions-with-same-name) – Bart van Ingen Schenau Jul 17 '23 at 07:10
  • Did you even read the question? I linked that answer in my post. – Marco Jul 17 '23 at 07: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. – Bart van Ingen Schenau Jul 17 '23 at 07: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. – Philip Kendall Jul 17 '23 at 07: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 '23 at 07:43

1 Answers1

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

gnasher729
  • 42,090
  • 4
  • 59
  • 119