6 ms·
Nice article. Saw a few things I wish I'd known about. 1. %n in printf would be handy when writing CLIs dealing w/ multiple lines or precise counts of backspac
by dantle 4y ago
Nice article. Saw a few things I wish I'd known about.
1. %n in printf would be handy when writing CLIs dealing w/ multiple lines or precise counts of backspaces.
2. Using enums as a form of static_assert() is a great idea (triggering a div by zero compiler error).
- torstenvl 4y ago%n is an extremely poor fit for CLI manipulation or tokenization for backspacing. %n is for bytes, not user-perceived characters.
- jcelerier 4y agousing enums as a form of static_assert is very bad when C nowadays literally has static_assert (_Static_assert: https://gcc.godbolt.org/z/bfv6rKdKM https://gcc.godbolt.org/z/bfv6rKdKM)
- tom_ 4y agoThe enum idea is interesting. I've previously used an extern with a conditional size of either 1 (valid) or -1 (invalid). This requires no additional boilerplate, and is #define-able into a static assert when built with a recent enough compiler. Something like this, from memory: #define STATIC_ASSERT(COND) extern char static_assert_cond_[(COND)?1:-1] /* C99 or earlier */ #define STATIC_ASSERT(COND) _Static_assert(COND) /* C11 or later */ As both are declarations, I don't think you'll end up in a situation where one is valid and the other isn't - but I could be wrong, and I suspect it would rarely matter in practice anyway.