The beauty and simplicity of the good old C-style void* in C++
- Programming
- Security
- Developer Tools
- Open Source
The post is a C++ style argument in favor of the old C idiom `void*` plus `size_t` for functions that take a memory blob. It claims this is simpler and more readable than signatures using `uint8_t*`, `std::byte`, or `std::span`, and treats the casts required by the more explicit versions as needless ugliness. That landed badly. The core pushback was that the article starts from the wrong premise. In idiomatic C++, most functions should not accept an untyped blob at all. They should accept the actual type they operate on, or a typed byte view if the function truly only cares about bytes.
If your API really consumes bytes, take a byte range type like `std::span<const std::byte>` or at least wrap pointer and length together. If you are tempted to hash, serialize, or reinterpret arbitrary structs by raw memory, stop and make the operation type-aware unless you can prove the representation is stable and meaningful.
-
giodicanio.com
- Discuss on HN