BoPeng/simuPOP

No access of members of vector<boo>::iterator with new libc++

BoPeng opened this issue · 1 comments

First, as explained here, a sad fact is that simuPOP has to use gcc's libstdc++, not LLVM's libc++. It is possible to use something else for binary vector in simuPOP but that would be a lot of work.

Second, mac's gcc is quite old (4.2.1) and will never be upgraded, and the toolchain has less and less support for gcc. At this point, with mac 10.13.3 (High Sierra), compiling simuPOP will show the following error:

$ python setup.py install
Can not obtain version of gcc, and openMP is disable
This version of boost is not tested. It may or may not work: boost_1_69_0
Building static libraries
In file included from boost_1_69_0/libs/serialization/src/codecvt_null.cpp:12:
In file included from boost_1_69_0/boost/archive/codecvt_null.hpp:19:
In file included from /usr/include/c++/4.2.1/locale:43:
In file included from /usr/include/c++/4.2.1/bits/localefwd.h:46:
In file included from /usr/include/c++/4.2.1/bits/c++locale.h:48:
/usr/include/c++/4.2.1/cstring:102:12: error: cannot initialize return object of type 'void *' with an rvalue of type
      'const void *'
  { return memchr(const_cast<const void*>(__p), __c, __n); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from boost_1_69_0/libs/serialization/src/codecvt_null.cpp:12:
In file included from boost_1_69_0/boost/archive/codecvt_null.hpp:19:
In file included from /usr/include/c++/4.2.1/locale:43:
In file included from /usr/include/c++/4.2.1/bits/localefwd.h:47:
In file included from /usr/include/c++/4.2.1/iosfwd:49:
In file included from /usr/include/c++/4.2.1/bits/postypes.h:46:
/usr/include/c++/4.2.1/cwchar:212:12: error: cannot initialize return object of type 'wchar_t *' with an rvalue of type
      'const wchar_t *'
  { return wcschr(const_cast<const wchar_t*>(__p), __c); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.2.1/cwchar:218:12: error: cannot initialize return object of type 'wchar_t *' with an rvalue of type
      'const wchar_t *'
  { return wcspbrk(const_cast<const wchar_t*>(__s1), __s2); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.2.1/cwchar:224:12: error: cannot initialize return object of type 'wchar_t *' with an rvalue of type
      'const wchar_t *'
  { return wcsrchr(const_cast<const wchar_t*>(__p), __c); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.2.1/cwchar:230:12: error: cannot initialize return object of type 'wchar_t *' with an rvalue of type
      'const wchar_t *'
  { return wcsstr(const_cast<const wchar_t*>(__s1), __s2); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.2.1/cwchar:236:12: error: cannot initialize return object of type 'wchar_t *' with an rvalue of type
      'const wchar_t *'
  { return wmemchr(const_cast<const wchar_t*>(__p), __c, __n); }
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from boost_1_69_0/libs/serialization/src/codecvt_null.cpp:12:
In file included from boost_1_69_0/boost/archive/codecvt_null.hpp:19:
In file included from /usr/include/c++/4.2.1/locale:44:
In file included from /usr/include/c++/4.2.1/bits/locale_classes.h:47:
In file included from /usr/include/c++/4.2.1/string:47:
In file included from /usr/include/c++/4.2.1/bits/char_traits.h:46:
In file included from /usr/include/c++/4.2.1/bits/stl_algobase.h:68:
/usr/include/c++/4.2.1/cstdlib:143:3: error: declaration conflicts with target of using declaration already in scope
  abs(long __i) { return labs(__i); }
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/stdlib.h:111:44: note: target of
      using declaration
inline _LIBCPP_INLINE_VISIBILITY long      abs(     long __x) _NOEXCEPT {return  labs(__x);}
                                           ^
/usr/include/c++/4.2.1/cstdlib:110:11: note: using declaration
  using ::abs;
          ^
/usr/include/c++/4.2.1/cstdlib:146:3: error: declaration conflicts with target of using declaration already in scope
  div(long __i, long __j) { return ldiv(__i, __j); }
  ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/stdlib.h:116:42: note: target of
      using declaration
inline _LIBCPP_INLINE_VISIBILITY  ldiv_t div(     long __x,      long __y) _NOEXCEPT {return  ldiv(__x, __y);}
                                         ^
/usr/include/c++/4.2.1/cstdlib:117:11: note: using declaration
  using ::div;
          ^
8 errors generated.
Failed to build a shared supporting library: command 'cc' failed with exit status 1

which I do not know how to fix.

Third, we are embedding boost 1.49.0 because some versions of boost do not work. The broken version should now be all gone so we should probably move away from embedded boost because it is difficult to compile. At the very least, we should remove bstd.cpp from iostreams because we do not need bstd support.

The problem is that vector<bool>::iterator, as defined here, is private. So there is no direct way to access the members as we have done for libstdc++.

To get around this, I am using an ugly hack that uses a class to mirror the members of this class, and use reinterpreter_cast to force the access of the members. A test program is as follows:

#include <vector>
#include <iostream>

using namespace std;

typedef vector<bool> vb;

// static_cast<unsigned>(sizeof(vb::__storage_type) * CHAR_BIT)


class my__bit_iterator
{
public:
    vb::__storage_type * __seg_;
    unsigned          __ctz_;
};

void something(vb & vec)
{
    vb::iterator it = vec.begin();
    vb::iterator it_end = vec.end();
    for (; it  != it_end; ++it) {
        cout << *it << " " << reinterpret_cast<my__bit_iterator*>(&it)->__seg_
            << " " << reinterpret_cast<my__bit_iterator*>(&it)->__ctz_ << endl;

    }

}

int main() {
    vb a;
    a.push_back(true);
    a.push_back(false);
    a.push_back(true);
    something(a);
    return 0;
}