Skip to content

Commit 6e4dad9

Browse files
laanwjcodablock
authored andcommitted
Merge bitcoin#8850: Implement (begin|end)_ptr in C++11 and add deprecation comment
f00705a serialize: Deprecate `begin_ptr` / `end_ptr` (Wladimir J. van der Laan) 47314e6 prevector: add C++11-like data() method (Wladimir J. van der Laan)
1 parent 14483e4 commit 6e4dad9

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/prevector.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,14 @@ class prevector {
475475
return ((size_t)(sizeof(T))) * _union.capacity;
476476
}
477477
}
478+
479+
value_type* data() {
480+
return item_ptr(0);
481+
}
482+
483+
const value_type* data() const {
484+
return item_ptr(0);
485+
}
478486
};
479487
#pragma pack(pop)
480488

src/serialize.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,32 @@ inline T* NCONST_PTR(const T* val)
4545
return const_cast<T*>(val);
4646
}
4747

48-
/**
49-
* Get begin pointer of vector (non-const version).
50-
* @note These functions avoid the undefined case of indexing into an empty
51-
* vector, as well as that of indexing after the end of the vector.
48+
/**
49+
* Important: Do not use the following functions in new code, but use v.data()
50+
* and v.data() + v.size() respectively directly. They were once introduced to
51+
* have a compatible, safe way to get the begin and end pointer of a vector.
52+
* However with C++11 the language has built-in functionality for this and it's
53+
* more readable to just use that.
5254
*/
5355
template <typename V>
5456
inline typename V::value_type* begin_ptr(V& v)
5557
{
56-
return v.empty() ? NULL : &v[0];
58+
return v.data();
5759
}
58-
/** Get begin pointer of vector (const version) */
5960
template <typename V>
6061
inline const typename V::value_type* begin_ptr(const V& v)
6162
{
62-
return v.empty() ? NULL : &v[0];
63+
return v.data();
6364
}
64-
/** Get end pointer of vector (non-const version) */
6565
template <typename V>
6666
inline typename V::value_type* end_ptr(V& v)
6767
{
68-
return v.empty() ? NULL : (&v[0] + v.size());
68+
return v.data() + v.size();
6969
}
70-
/** Get end pointer of vector (const version) */
7170
template <typename V>
7271
inline const typename V::value_type* end_ptr(const V& v)
7372
{
74-
return v.empty() ? NULL : (&v[0] + v.size());
73+
return v.data() + v.size();
7574
}
7675

7776
/*

0 commit comments

Comments
 (0)