1

Due to some legacy reasons I'm stuck with MIPS-GCC 4.5.3. But the code which I'm trying to compile uses C++11 nullptr & nullptr_t heavily which is a missing feature in GCC 4.5.3.

After some googling & getting into the usage I ended up creating a nullptr wrapper like below, but unfortunately it doesn't satisfy some of the use case,

namespace std {

class nullptr_t {
public:

nullptr_t() { }
template <typename T> nullptr_t(const T&) { }
template <class T> nullptr_t(const T*) { }
template <class T> nullptr_t(T*) { }
template <typename T, typename  U> nullptr_t(const typename T::U*) { }

template<typename T> operator T*() { return 0;}
template<typename T1, typename T2> operator T1 T2::*() { return 0; }

operator int() const { return 0; }
operator unsigned() const { return 0; }
operator bool() const { return false; }
bool operator == (unsigned i) const { return i == 0; }
bool operator != (unsigned i) const { return i != 0; }
bool operator !() const { return true; }

} nullptr = {};

}

using std::nullptr;

template<typename T> struct DummyContainer {
  DummyContainer(T* ptr)
    : m_ptr(ptr) { }

  DummyContainer(std::nullptr_t)
    : m_ptr(0) { }

  T& operator = (std::nullptr_t)  { return *m_ptr; }

  private: T* m_ptr;
};

int main(int argc, char** argv)
{
  const char* case1 = nullptr; // working
  // I think for below case std::unique_ptr has to be modified to take std::nullptr_t during construction & operator =
  std::unique_ptr<char> case2 =  nullptr; // not working. 
  DummyContainer<char> case3 = nullptr; // working
  case3 = nullptr; //working
  unsigned* case4 = argc > 1 ? nullptr : nullptr; //works


  unsigned* case5 = argc > 2 ? (unsigned*)0 : nullptr; //not working. (It is the major issue as of now)

  return 0;
}

Here the major case is unsigned* case5 = argc > 2 ? (unsigned*)0 : nullptr;

IDEONE snapshot : http://ideone.com/m1mhtB

(Thanks to http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf)

Any tips/suggestions will be appreciated :)

(Note: please avoid answers like upgrade your gcc)

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
Arunprasad Rajkumar
  • 1,374
  • 1
  • 15
  • 31
  • I'm not sure that I understand why you cannot just define nullptr to (void*)NULL and nullptr_t to void*? – Iłya Bursov Oct 22 '13 at 19:23
  • @ unsigned* case4 = argc > 1 ? (unsigned*)0 : (void*)0; won't work right? – Arunprasad Rajkumar Oct 22 '13 at 19:38
  • yes, this will produce just 0, but it will be the same for nullptr, or I'm missing something about c++11? – Iłya Bursov Oct 22 '13 at 19:40
  • Doesn't [Scott Meyer's implementation](http://stackoverflow.com/a/700412/572743) work with gcc 4.5? – Damon Oct 22 '13 at 20:59
  • Spend your energy and time on compiling a recent GCC 4.9.1 for your system. It is the most sensible thing to do. Don't dare using GCC 4.5 for C++11 (since that old compiler was released before the standard). Or else, stick to C++03 and don't use `nullptr`; C++11 is much more than `nullptr` – Basile Starynkevitch Jul 30 '14 at 13:08
  • That is a platform issue. The chipset vendor is not ready to support latest toolchains.(Ours is not x86, it is a mipsel) So we stuck with 4.5.x. I'm trying llvm + clang based solutions. Not sure how far it is feasible. http://stackoverflow.com/questions/24975129/how-to-compile-and-run-llc-3-4-generated-c-code-using-native-compilerg – Arunprasad Rajkumar Jul 30 '14 at 14:13

1 Answers1

3

Below solution seems to be working,

Original source: https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/wtf/NullPtr.h

namespace std {
class nullptr_t {
public:
    // Required in order to create const nullptr_t objects without an
    // explicit initializer in GCC 4.5, a la:
    //
    // const std::nullptr_t nullptr;
    nullptr_t() { }

    // Make nullptr convertible to any pointer type.
    template<typename T> operator T*() const { return 0; }
    // Make nullptr convertible to any member pointer type.
    template<typename C, typename T> operator T C::*() { return 0; }
private:
    // Do not allow taking the address of nullptr.
    void operator&();
};
}

const std::nullptr_t nullptr;

IDEOne: http://ideone.com/Bnp6th

Arunprasad Rajkumar
  • 1,374
  • 1
  • 15
  • 31