aboutsummaryrefslogtreecommitdiff
path: root/src/uint256.h
diff options
context:
space:
mode:
authorAnthony Towns <aj@erisian.com.au>2020-09-25 15:01:38 +1000
committerAnthony Towns <aj@erisian.com.au>2020-09-28 12:14:19 +1000
commit183f308fff4caad3e3ada654b6fdf597d262c4c1 (patch)
tree2cabf5cc8477e418159670ed73be97daa96b0bdd /src/uint256.h
parent78f912c9010f686e2d1bbdc1c51f381b496c2a1b (diff)
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.h17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/uint256.h b/src/uint256.h
index 8ab747ef49..3e245763b3 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