由于一些遗留原因,我坚持使用 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这样的答案)