// OPT: -lncurses -Wno-conversion-null // sample code to explain why C++11 now has nullptr #include #include #include #include using namespace std; string makestr(int i) { stringstream ss; ss << i; return ss.str(); } string makestr(long i) { stringstream ss; ss << i << 'L'; return ss.str(); } string makestr(char *s) { return string(s ? s : ""); } template void display(T *data, int n) { if (data) for(int i=0;i // D is an object that can draw // P is a pointer to an object that D can draw void draworigin(D drawer, P obj) { drawer.move_pen(0,0); drawer.draw(obj); } // uses the curses library class asciiart { public: asciiart() { initscr(); } ~asciiart() { endwin(); } void move_pen(int x, int y) { move(y,x); } void draw(const char *s) { if (s) printw(s); getch(); // to pause } private: WINDOW *w; }; int main(int argc, char **argv) { cout << "line 1: " << makestr(NULL) << endl; cout << "line 2: " << makestr(0) << endl; cout << "line 3: " << makestr(nullptr) << endl; if (nullptr) cout << "should not get here" << endl; else cout << "nullptr convertable to bool" << endl; int num[4] {1,3,2,4}; display(num,4); //display(nullptr,0); // won't compile -- cannot determine type of T //display(NULL,0); // won't compile -- same problem char ch; cin >> noskipws >> ch; asciiart aa; draworigin(aa,"hello"); draworigin(aa,nullptr); draworigin(aa,"bye"); //draworigin(aa,NULL); // this line won't compile -- asciiart doesn't // have a draw method for int }