1

由于一些遗留原因,我坚持使用 MIPS-GCC 4.5.3。但是我试图编译的代码大量使用了 C++11 nullptr & nullptr_t,这是 GCC 4.5.3 中缺少的功能。

经过一番谷歌搜索并开始使用后,我最终创建了一个nullptr wrapper如下所示的内容,但不幸的是它不能满足某些用例,

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;
}

这里的主要情况是unsigned* case5 = argc > 2 ? (unsigned*)0 : nullptr;

IDEONE 快照:http: //ideone.com/m1mhtB

(感谢http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2431.pdf

任何提示/建议将不胜感激:)

(注意:请避免像升级你的 gcc这样的答案)

4

1 回答 1

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

于 2013-10-22T19:53:12.810 回答