logoalt Hacker News

trumpdongtoday at 10:45 AM2 repliesview on HN

I find this to be a snarky non-answer. You really think everyone should write their own memcpy for every POD type they want to memcpy?


Replies

mfosttoday at 11:00 AM

There's no need: there's std::copy already.

Or maybe the idea was to create a typesafe template wrapper around the generic function which is also very common and really nice. No need to create one wrapper per type, a single template should work.

show 1 reply
AnimalMuppettoday at 3:00 PM

In addition to what mfrost said, there's also no need because C++ assignment is member-by-member copy unless otherwise specifically implemented for the type. If you have a POD, then that's what you get with assignment; there's no need to call memcpy at all.

(The difference is that memcpy will copy padding bytes, and the assignment operator may not. But if you depend on the values of the padding bytes, you have major problems...)