From 207efca15cf471bbc2990cbd336000e04dcfcd22 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Thu, 4 Jan 2018 10:28:34 -0800 Subject: [PATCH] workaround for msvc pickyness The MSVC STL implementation asserts that pointer/itertors are not null, even when they would not be dereferenced (eg. empty input range). --- src/misc/pv/sharedVector.h | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/misc/pv/sharedVector.h b/src/misc/pv/sharedVector.h index b5e0fcd..96efaa2 100644 --- a/src/misc/pv/sharedVector.h +++ b/src/misc/pv/sharedVector.h @@ -455,13 +455,27 @@ public: this->m_offset=0; } - +private: + /* Hack alert. + * For reasons of simplicity and efficiency, we want to use raw pointers for iteration. + * However, shared_ptr::get() isn't defined when !m_sdata, although practically it gives NULL. + * Unfortunately, many of the MSVC STL methods assert() that iterators are never NULL. + * So we fudge here by abusing 'this' so that our iterators are always !NULL. + */ + inline E* base_ptr() const { +#ifdef _MSC_VER + return this->m_count ? this->m_sdata.get() : (E*)(this-1); +#else + return this->m_sdata.get(); +#endif + } +public: // STL iterators - iterator begin() const{return this->m_sdata.get()+this->m_offset;} + iterator begin() const{return this->base_ptr()+this->m_offset;} const_iterator cbegin() const{return begin();} - iterator end() const{return this->m_sdata.get()+this->m_offset+this->m_count;} + iterator end() const{return this->base_ptr()+this->m_offset+this->m_count;} const_iterator cend() const{return end();} reverse_iterator rbegin() const{return reverse_iterator(end());}