2 ms·
> While we’re on the topic of little annoyances, another pattern I often see is using a variable just to pass what is effectively a literal to setsockopt():
by kazinator 8y ago
> While we’re on the topic of little annoyances, another pattern I often see is using a variable just to pass what is effectively a literal to setsockopt():
int yes=1;
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (yes)) ;
> Which can instead be written as:
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &(int) {1}, sizeof(int));
>
I'm sorry, no it cannot. Or, rather, not without
1. Switching C90 code to C99; or
2. Switching C++ code to C99.
Good luck with 2.
GCC and Clang support this in C++ as an extension; then you're not writing in standard C++ any longer.
1 is a nonstarter in project that mandates C90.
In other words, there may be valid reasons why the code is that way other than ignorance of compound literals.