Reading the code of the C library’s sockets interface, I found this:
/* Types of sockets. */
enum __socket_type
{
SOCK_STREAM = 1, /* Sequenced, reliable, connection-based
byte streams. */
#define SOCK_STREAM SOCK_STREAM
SOCK_DGRAM = 2, /* Connectionless, unreliable datagrams
of fixed maximum length. */
#define SOCK_DGRAM SOCK_DGRAM
...
This “idiom” is used all over bits/socket.h. I am just curious, what is the purpose of those macros?
These constants used to just be
#defines, so what you’re seeing is probably there to protect you from accidentally mixing old and new header files. An advantage of usingenumto define the constants is thatenummembers tend to be available in the debugger, and#definemacros don’t.If you were to (accidentally) include some other header file that also tries to
#define SOCK_STREAM, you would get a compiler warning instead of silently using a possibly incorrect value.UPDATE
Looking through a glibc git repo, I found the specific commit that added the
#defines, with this comment:There you have it.