diff options
| author | Anthony Towns <[email protected]> | 2020-09-25 15:01:38 +1000 |
|---|---|---|
| committer | Anthony Towns <[email protected]> | 2020-09-28 12:14:19 +1000 |
| commit | 183f308fff4caad3e3ada654b6fdf597d262c4c1 (patch) | |
| tree | 2cabf5cc8477e418159670ed73be97daa96b0bdd /src/uint256.h | |
| parent | Merge #19804: test/refactor: reference p2p objects explicitly and remove conf... (diff) | |
| download | discoin-183f308fff4caad3e3ada654b6fdf597d262c4c1.tar.xz discoin-183f308fff4caad3e3ada654b6fdf597d262c4c1.zip | |
uint256: Update constructors to c++11, make ONE static
Replace the memset with C++11 value/aggregate initialisation of
the m_data array, which still ensures the unspecified values end
up as zero-initialised.
This then allows changing UINT256_ONE() from dynamically allocating an
object, to a simpler referencing a static allocation.
Diffstat (limited to 'src/uint256.h')
| -rw-r--r-- | src/uint256.h | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/src/uint256.h b/src/uint256.h index 8ab747ef4..3e245763b 100644 --- a/src/uint256.h +++ b/src/uint256.h @@ -20,10 +20,11 @@ protected: static constexpr int WIDTH = BITS / 8; uint8_t m_data[WIDTH]; public: - base_blob() - { - memset(m_data, 0, sizeof(m_data)); - } + /* construct 0 value by default */ + constexpr base_blob() : m_data() {} + + /* constructor for constants between 1 and 255 */ + constexpr explicit base_blob(uint8_t v) : m_data{v} {} explicit base_blob(const std::vector<unsigned char>& vch); @@ -111,7 +112,7 @@ public: */ class uint160 : public base_blob<160> { public: - uint160() {} + constexpr uint160() {} explicit uint160(const std::vector<unsigned char>& vch) : base_blob<160>(vch) {} }; @@ -122,8 +123,10 @@ public: */ class uint256 : public base_blob<256> { public: - uint256() {} + constexpr uint256() {} + constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {} explicit uint256(const std::vector<unsigned char>& vch) : base_blob<256>(vch) {} + static const uint256 ONE; }; /* uint256 from const char *. @@ -147,6 +150,6 @@ inline uint256 uint256S(const std::string& str) return rv; } -uint256& UINT256_ONE(); +inline const uint256& UINT256_ONE() { return uint256::ONE; } #endif // BITCOIN_UINT256_H |