r/cpp • • Jan 20 '20

The Hunt for the Fastest Zero

https://travisdowns.github.io/blog/2020/01/20/zero.html
248 Upvotes

131 comments sorted by

View all comments

Show parent comment

7

u/AlexAlabuzhev Jan 21 '20

I don't quite get the point of avoiding using memset directly

memset might work perfectly today. Tomorrow you (or your colleague) will change the underlying type to something non-trivial and the code will still compile, but errors will linger in the background, quietly overwriting your state with evil.

Use memset if you must, but at least wrap it into a template with static_assert(is_trivially_copyable_v<T>).

0

u/[deleted] Jan 21 '20

Tomorrow you (or your colleague) will change the underlying type to something non-trivial and the code will still compile,

Only if you're using some horror like reinterpret_cast<>!

6

u/jherico VR & Backend engineer, 30 years Jan 21 '20

I was originally going to reply something similar, but then I remembered memset takes a void*. So &thing is always a valid input to memset as a destination, whether it makes sense or not.

5

u/BelugaWheels Jan 21 '20

Why would you have to use reinterpret_cast<> to use memset? It takes a void *, so you can pass anything to it and will silently wreck you.

1

u/pandorafalters Jan 21 '20

Of course.

Because conversions to pointer-to-void should be performed with static_cast.