fork(3) download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. // #define USE_CXX11_NULLPTR
  5.  
  6. #ifndef USE_CXX11_NULLPTR
  7. ////////////////////////////////////////////
  8. // To avoid errors in latest GCC which is used by IDEOne.
  9. // In my case I use GCC 4.5.3 where it supports some features of C++11, but not nullptr.
  10. #define nullptr nullptr__
  11. #define nullptr_t nullptr_t__
  12. ////////////////////////////////////////////
  13.  
  14. namespace std {
  15.  
  16. class nullptr_t {
  17. public:
  18.  
  19. nullptr_t() { }
  20. template <typename T> nullptr_t(const T&) { }
  21. template <class T> nullptr_t(const T*) { }
  22. template <class T> nullptr_t(T*) { }
  23. template <typename T, typename U> nullptr_t(const typename T::U*) { }
  24.  
  25. template<typename T> operator T*() { return 0;}
  26. template<typename T1, typename T2> operator T1 T2::*() { return 0; }
  27.  
  28. operator int() const { return 0; }
  29. operator unsigned() const { return 0; }
  30. operator bool() const { return false; }
  31. bool operator == (unsigned i) const { return i == 0; }
  32. bool operator != (unsigned i) const { return i != 0; }
  33. bool operator !() const { return true; }
  34.  
  35. } nullptr = {};
  36.  
  37. }
  38.  
  39. using std::nullptr;
  40.  
  41. #endif //USE_CXX11_NULLPTR
  42.  
  43. template<typename T> struct DummyContainer {
  44. DummyContainer(T* ptr)
  45. : m_ptr(ptr) { }
  46.  
  47. DummyContainer(std::nullptr_t)
  48. : m_ptr(0) { }
  49.  
  50. T& operator = (std::nullptr_t) { return *m_ptr; }
  51.  
  52. private: T* m_ptr;
  53. };
  54.  
  55. int main(int argc, char** argv)
  56. {
  57. const char* case1 = nullptr; // working
  58. // I think for below case std::unique_ptr has to be modified to take std::nullptr_t during construction & operator =
  59. std::unique_ptr<char> case2 = nullptr; // not working.
  60. DummyContainer<char> case3 = nullptr; // working
  61. case3 = nullptr; //working
  62. unsigned* case4 = argc > 1 ? nullptr : nullptr; //works
  63. unsigned* case5 = argc > 2 ? (unsigned*)0 : nullptr; //not working. (It is the major issue as of now)
  64.  
  65. return 0;
  66. }
Compilation error #stdin compilation error #stdout 0s 3292KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main(int, char**)’:
prog.cpp:10:17: error: conversion from ‘std::nullptr_t__’ to non-scalar type ‘std::unique_ptr<char>’ requested
 #define nullptr nullptr__
                 ^
prog.cpp:59:33: note: in expansion of macro ‘nullptr’
  std::unique_ptr<char> case2 =  nullptr; // not working. 
                                 ^
prog.cpp:10:17: error: operands to ?: have different types ‘unsigned int*’ and ‘std::nullptr_t__’
 #define nullptr nullptr__
                 ^
prog.cpp:63:46: note: in expansion of macro ‘nullptr’
  unsigned* case5 = argc > 2 ? (unsigned*)0 : nullptr; //not working. (It is the major issue as of now)
                                              ^
prog.cpp:57:14: warning: unused variable ‘case1’ [-Wunused-variable]
  const char* case1 = nullptr; // working
              ^
prog.cpp:62:12: warning: unused variable ‘case4’ [-Wunused-variable]
  unsigned* case4 = argc > 1 ? nullptr : nullptr; //works
            ^
prog.cpp:63:12: warning: unused variable ‘case5’ [-Wunused-variable]
  unsigned* case5 = argc > 2 ? (unsigned*)0 : nullptr; //not working. (It is the major issue as of now)
            ^
stdout
Standard output is empty