From 47314e6daad8157c9e36af90a47b3ae7fa0587de Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Fri, 30 Sep 2016 17:19:51 +0200 Subject: [PATCH 1/2] prevector: add C++11-like data() method This returns a pointer to the beginning of the vector's data. --- src/prevector.h | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/prevector.h b/src/prevector.h index a0e1e140b4..d840dfcd08 100644 --- a/src/prevector.h +++ b/src/prevector.h @@ -475,6 +475,14 @@ public: return ((size_t)(sizeof(T))) * _union.capacity; } } + + value_type* data() noexcept { + return item_ptr(0); + } + + const value_type* data() const { + return item_ptr(0); + } }; #pragma pack(pop) From f00705ae7ff3c1db805859e3be41e58ac70625b6 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Fri, 30 Sep 2016 17:21:12 +0200 Subject: [PATCH 2/2] serialize: Deprecate `begin_ptr` / `end_ptr` Implement `begin_ptr` and `end_ptr` in terms of C++11 code, and add a comment that they are deprecated. Follow-up to developer notes update in 654a21162252294b7dbd6c982fec88008af7335e. --- src/prevector.h | 2 +- src/serialize.h | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/prevector.h b/src/prevector.h index d840dfcd08..25bce522dc 100644 --- a/src/prevector.h +++ b/src/prevector.h @@ -476,7 +476,7 @@ public: } } - value_type* data() noexcept { + value_type* data() { return item_ptr(0); } diff --git a/src/serialize.h b/src/serialize.h index 04ab9aa2e7..1f51da82ff 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -44,33 +44,32 @@ inline T* NCONST_PTR(const T* val) return const_cast(val); } -/** - * Get begin pointer of vector (non-const version). - * @note These functions avoid the undefined case of indexing into an empty - * vector, as well as that of indexing after the end of the vector. +/** + * Important: Do not use the following functions in new code, but use v.data() + * and v.data() + v.size() respectively directly. They were once introduced to + * have a compatible, safe way to get the begin and end pointer of a vector. + * However with C++11 the language has built-in functionality for this and it's + * more readable to just use that. */ template inline typename V::value_type* begin_ptr(V& v) { - return v.empty() ? NULL : &v[0]; + return v.data(); } -/** Get begin pointer of vector (const version) */ template inline const typename V::value_type* begin_ptr(const V& v) { - return v.empty() ? NULL : &v[0]; + return v.data(); } -/** Get end pointer of vector (non-const version) */ template inline typename V::value_type* end_ptr(V& v) { - return v.empty() ? NULL : (&v[0] + v.size()); + return v.data() + v.size(); } -/** Get end pointer of vector (const version) */ template inline const typename V::value_type* end_ptr(const V& v) { - return v.empty() ? NULL : (&v[0] + v.size()); + return v.data() + v.size(); } /*