Обложка канала

Библиотека C/C++ разработчика

14677 @cppproglib

Полезные материалы по всему, что может быть полезно плюсовику/сишнику.

Библиотека C/C++ разработчика

4 года назад
Открыть в
`saturating_add` vs. `saturating_int` -- new function vs. new type?

Suppose you want to do integer arithmetic that saturates instead of overflowing. The built-in operator+ doesn’t behave that way, so you need to roll something yourself. Do you write a saturating_add() function or a new saturating_int type with overloaded operator+? What about atomic_load(x) vs. atomic x? Or volatile_store(ptr, value) vs. volatile int*? When should you provide functions that implement new behavior and when should you write a wrapper type? Let’s look at the pro and cons.

www.foonathan.net