Skip to content

Commit 7dce175

Browse files
committed
Merge #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)
2 parents a7e5cbb + f00705a commit 7dce175

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
@@ -44,33 +44,32 @@ inline T* NCONST_PTR(const T* val)
4444
return const_cast<T*>(val);
4545
}
4646

47-
/**
48-
* Get begin pointer of vector (non-const version).
49-
* @note These functions avoid the undefined case of indexing into an empty
50-
* vector, as well as that of indexing after the end of the vector.
47+
/**
48+
* Important: Do not use the following functions in new code, but use v.data()
49+
* and v.data() + v.size() respectively directly. They were once introduced to
50+
* have a compatible, safe way to get the begin and end pointer of a vector.
51+
* However with C++11 the language has built-in functionality for this and it's
52+
* more readable to just use that.
5153
*/
5254
template <typename V>
5355
inline typename V::value_type* begin_ptr(V& v)
5456
{
55-
return v.empty() ? NULL : &v[0];
57+
return v.data();
5658
}
57-
/** Get begin pointer of vector (const version) */
5859
template <typename V>
5960
inline const typename V::value_type* begin_ptr(const V& v)
6061
{
61-
return v.empty() ? NULL : &v[0];
62+
return v.data();
6263
}
63-
/** Get end pointer of vector (non-const version) */
6464
template <typename V>
6565
inline typename V::value_type* end_ptr(V& v)
6666
{
67-
return v.empty() ? NULL : (&v[0] + v.size());
67+
return v.data() + v.size();
6868
}
69-
/** Get end pointer of vector (const version) */
7069
template <typename V>
7170
inline const typename V::value_type* end_ptr(const V& v)
7271
{
73-
return v.empty() ? NULL : (&v[0] + v.size());
72+
return v.data() + v.size();
7473
}
7574

7675
/*

0 commit comments

Comments
 (0)