Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int* p1 = nullptr;
- int* p2 = NULL;
- int* p3 = 0;
- void f(char const *ptr);
- void f(int v);
- f(NULL); //which function will be called?
- f(nullptr); //first function is called
- template<typename T, T *ptr>
- struct something{}; //primary template
- template<>
- struct something<nullptr_t, nullptr>{}; //partial specialization for nullptr
- template<typename T>
- void f(T *ptr); //function to handle non-nullptr argument
- void f(nullptr_t); //an overload to handle nullptr argument!!!
- int main()
- {
- int *ptr = NULL;
- return 0;
- }
- prog.cpp: In function 'int main()':
- prog.cpp:3:16: error: 'NULL' was not declared in this scope
- std::string * str = NULL; //Case 1
- void (A::*ptrFunc) () = &A::doSomething;
- if (ptrFunc == NULL) {} //Case 2
- #include<iostream>
- void doSomething(int)
- {
- std::cout<<"In Int version";
- }
- void doSomething(char *)
- {
- std::cout<<"In char* version";
- }
- int main()
- {
- doSomething(NULL);
- return 0;
- }
- In Int version
- #include <cstddef>
- void doSomething(int);
- void doSomething(char *);
- int main()
- {
- doSomething(static_cast <char *>(0)); // Case 1
- doSomething(0); // Case 2
- doSomething(NULL) // Case 3
- }
- #include<iostream>
- void doSomething(int)
- {
- std::cout<<"In Int version";
- }
- void doSomething(char *)
- {
- std::cout<<"In char* version";
- }
- int main()
- {
- char *pc = nullptr; // Case 1
- int i = nullptr; // Case 2
- bool flag = nullptr; // Case 3
- doSomething(nullptr); // Case 4
- return 0;
- }
- void f(int* p);
- template<typename T> void forward(T&& t) {
- f(std::forward<T>(t));
- }
- int main() {
- forward(0); // FAIL
- }
- void f(int);
- void f(int*);
- int main() { f(0); f(nullptr); }
- void f(int*);
- void f(long*);
- int main() { f(0); }
- void f(std::nullptr_t)
- int main() { f(nullptr); }
- void fun(int); // two overloads of fun
- void fun(void*);
- fun(0); // calls f(int), not fun(void*)
- fun(NULL); // might not compile, but typically calls fun(int). Never calls fun(void*)
- fun(nullptr); // calls fun(void*) overload
- auto result = findRecord( /* arguments */ );
- if (result == 0) {
- ....
- }
- auto result = findRecord( /* arguments */ );
- if (result == nullptr) {
- ...
- }
- #include<iostream>
- #include <memory>
- #include <thread>
- #include <mutex>
- using namespace std;
- int f1(std::shared_ptr<int> spw) // call these only when
- {
- //do something
- return 0;
- }
- double f2(std::unique_ptr<int> upw) // the appropriate
- {
- //do something
- return 0.0;
- }
- bool f3(int* pw) // mutex is locked
- {
- return 0;
- }
- std::mutex f1m, f2m, f3m; // mutexes for f1, f2, and f3
- using MuxtexGuard = std::lock_guard<std::mutex>;
- void lockAndCallF1()
- {
- MuxtexGuard g(f1m); // lock mutex for f1
- auto result = f1(static_cast<int>(0)); // pass 0 as null ptr to f1
- cout<< result<<endl;
- }
- void lockAndCallF2()
- {
- MuxtexGuard g(f2m); // lock mutex for f2
- auto result = f2(static_cast<int>(NULL)); // pass NULL as null ptr to f2
- cout<< result<<endl;
- }
- void lockAndCallF3()
- {
- MuxtexGuard g(f3m); // lock mutex for f2
- auto result = f3(nullptr);// pass nullptr as null ptr to f3
- cout<< result<<endl;
- } // unlock mutex
- int main()
- {
- lockAndCallF1();
- lockAndCallF2();
- lockAndCallF3();
- return 0;
- }
- #include<iostream>
- #include <memory>
- #include <thread>
- #include <mutex>
- using namespace std;
- int f1(std::shared_ptr<int> spw) // call these only when
- {
- //do something
- return 0;
- }
- double f2(std::unique_ptr<int> upw) // the appropriate
- {
- //do something
- return 0.0;
- }
- bool f3(int* pw) // mutex is locked
- {
- return 0;
- }
- std::mutex f1m, f2m, f3m; // mutexes for f1, f2, and f3
- using MuxtexGuard = std::lock_guard<std::mutex>;
- template<typename FuncType, typename MuxType, typename PtrType>
- auto lockAndCall(FuncType func, MuxType& mutex, PtrType ptr) -> decltype(func(ptr))
- //decltype(auto) lockAndCall(FuncType func, MuxType& mutex, PtrType ptr)
- {
- MuxtexGuard g(mutex);
- return func(ptr);
- }
- int main()
- {
- auto result1 = lockAndCall(f1, f1m, 0); //compilation failed
- //do something
- auto result2 = lockAndCall(f2, f2m, NULL); //compilation failed
- //do something
- auto result3 = lockAndCall(f3, f3m, nullptr);
- //do something
- return 0;
- }
- void foo(int);
- void foo(int*);
- foo((int*)0); // note: foo(NULL) means foo(0)
- foo(nullptr);
- auto p = 0; // makes auto as int
- auto p = nullptr; // makes auto as decltype(nullptr)
- MyClass *arr[4];
- std::fill_n(arr, 4, NULL);
Add Comment
Please, Sign In to add comment