rescrv / po6

POSIX wrappers for C++
BSD 3-Clause "New" or "Revised" License
30 stars 15 forks source link

Turn errno into a switch case #10

Closed wijagels closed 6 years ago

wijagels commented 7 years ago

Godbolt for difference

rescrv commented 7 years ago

Can you provide some context for changing things? I cannot remember why I chose using if instead of case, but the fact that I use case in all other areas where I defined the CSTRINGIFY macro (e.g., https://github.com/rescrv/Replicant/blob/master/common/macros.h) implies to me that I made a conscious choice of if vs. switch at the time.

wijagels commented 7 years ago

Sure, the idea is to make it easier for the compiler to optimize strerror. Using a switch also forces any dead-code to be removed, since collisions must be explicitly resolved, instead of creating an unreachable branch. The likely reason for using an if is that there are a few error codes that collide such as EDEADLOCK and EDEADLK (POSIX). Linux violates POSIX by having EWOULDBLOCK equal EAGAIN. I didn't see any other collisions, but checking other systems is a good idea.