Guest User

Untitled

a guest
Jul 17th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. int* p1 = nullptr;
  2. int* p2 = NULL;
  3. int* p3 = 0;
  4.  
  5. void f(char const *ptr);
  6. void f(int v);
  7.  
  8. f(NULL); //which function will be called?
  9.  
  10. f(nullptr); //first function is called
  11.  
  12. template<typename T, T *ptr>
  13. struct something{}; //primary template
  14.  
  15. template<>
  16. struct something<nullptr_t, nullptr>{}; //partial specialization for nullptr
  17.  
  18. template<typename T>
  19. void f(T *ptr); //function to handle non-nullptr argument
  20.  
  21. void f(nullptr_t); //an overload to handle nullptr argument!!!
  22.  
  23. int main()
  24. {
  25. int *ptr = NULL;
  26. return 0;
  27. }
  28.  
  29. prog.cpp: In function 'int main()':
  30. prog.cpp:3:16: error: 'NULL' was not declared in this scope
  31.  
  32. std::string * str = NULL; //Case 1
  33. void (A::*ptrFunc) () = &A::doSomething;
  34. if (ptrFunc == NULL) {} //Case 2
  35.  
  36. #include<iostream>
  37. void doSomething(int)
  38. {
  39. std::cout<<"In Int version";
  40. }
  41. void doSomething(char *)
  42. {
  43. std::cout<<"In char* version";
  44. }
  45.  
  46. int main()
  47. {
  48. doSomething(NULL);
  49. return 0;
  50. }
  51.  
  52. In Int version
  53.  
  54. #include <cstddef>
  55.  
  56. void doSomething(int);
  57. void doSomething(char *);
  58.  
  59. int main()
  60. {
  61. doSomething(static_cast <char *>(0)); // Case 1
  62. doSomething(0); // Case 2
  63. doSomething(NULL) // Case 3
  64. }
  65.  
  66. #include<iostream>
  67. void doSomething(int)
  68. {
  69. std::cout<<"In Int version";
  70. }
  71. void doSomething(char *)
  72. {
  73. std::cout<<"In char* version";
  74. }
  75.  
  76. int main()
  77. {
  78. char *pc = nullptr; // Case 1
  79. int i = nullptr; // Case 2
  80. bool flag = nullptr; // Case 3
  81.  
  82. doSomething(nullptr); // Case 4
  83. return 0;
  84. }
  85.  
  86. void f(int* p);
  87. template<typename T> void forward(T&& t) {
  88. f(std::forward<T>(t));
  89. }
  90. int main() {
  91. forward(0); // FAIL
  92. }
  93.  
  94. void f(int);
  95. void f(int*);
  96. int main() { f(0); f(nullptr); }
  97.  
  98. void f(int*);
  99. void f(long*);
  100. int main() { f(0); }
  101.  
  102. void f(std::nullptr_t)
  103. int main() { f(nullptr); }
  104.  
  105. void fun(int); // two overloads of fun
  106. void fun(void*);
  107. fun(0); // calls f(int), not fun(void*)
  108. fun(NULL); // might not compile, but typically calls fun(int). Never calls fun(void*)
  109.  
  110. fun(nullptr); // calls fun(void*) overload
  111.  
  112. auto result = findRecord( /* arguments */ );
  113. if (result == 0) {
  114. ....
  115. }
  116.  
  117. auto result = findRecord( /* arguments */ );
  118. if (result == nullptr) {
  119. ...
  120. }
  121.  
  122. #include<iostream>
  123. #include <memory>
  124. #include <thread>
  125. #include <mutex>
  126. using namespace std;
  127. int f1(std::shared_ptr<int> spw) // call these only when
  128. {
  129. //do something
  130. return 0;
  131. }
  132. double f2(std::unique_ptr<int> upw) // the appropriate
  133. {
  134. //do something
  135. return 0.0;
  136. }
  137. bool f3(int* pw) // mutex is locked
  138. {
  139.  
  140. return 0;
  141. }
  142.  
  143. std::mutex f1m, f2m, f3m; // mutexes for f1, f2, and f3
  144. using MuxtexGuard = std::lock_guard<std::mutex>;
  145.  
  146. void lockAndCallF1()
  147. {
  148. MuxtexGuard g(f1m); // lock mutex for f1
  149. auto result = f1(static_cast<int>(0)); // pass 0 as null ptr to f1
  150. cout<< result<<endl;
  151. }
  152.  
  153. void lockAndCallF2()
  154. {
  155. MuxtexGuard g(f2m); // lock mutex for f2
  156. auto result = f2(static_cast<int>(NULL)); // pass NULL as null ptr to f2
  157. cout<< result<<endl;
  158. }
  159. void lockAndCallF3()
  160. {
  161. MuxtexGuard g(f3m); // lock mutex for f2
  162. auto result = f3(nullptr);// pass nullptr as null ptr to f3
  163. cout<< result<<endl;
  164. } // unlock mutex
  165. int main()
  166. {
  167. lockAndCallF1();
  168. lockAndCallF2();
  169. lockAndCallF3();
  170. return 0;
  171. }
  172.  
  173. #include<iostream>
  174. #include <memory>
  175. #include <thread>
  176. #include <mutex>
  177. using namespace std;
  178. int f1(std::shared_ptr<int> spw) // call these only when
  179. {
  180. //do something
  181. return 0;
  182. }
  183. double f2(std::unique_ptr<int> upw) // the appropriate
  184. {
  185. //do something
  186. return 0.0;
  187. }
  188. bool f3(int* pw) // mutex is locked
  189. {
  190.  
  191. return 0;
  192. }
  193.  
  194. std::mutex f1m, f2m, f3m; // mutexes for f1, f2, and f3
  195. using MuxtexGuard = std::lock_guard<std::mutex>;
  196.  
  197. template<typename FuncType, typename MuxType, typename PtrType>
  198. auto lockAndCall(FuncType func, MuxType& mutex, PtrType ptr) -> decltype(func(ptr))
  199. //decltype(auto) lockAndCall(FuncType func, MuxType& mutex, PtrType ptr)
  200. {
  201. MuxtexGuard g(mutex);
  202. return func(ptr);
  203. }
  204. int main()
  205. {
  206. auto result1 = lockAndCall(f1, f1m, 0); //compilation failed
  207. //do something
  208. auto result2 = lockAndCall(f2, f2m, NULL); //compilation failed
  209. //do something
  210. auto result3 = lockAndCall(f3, f3m, nullptr);
  211. //do something
  212. return 0;
  213. }
  214.  
  215. void foo(int);
  216. void foo(int*);
  217.  
  218. foo((int*)0); // note: foo(NULL) means foo(0)
  219.  
  220. foo(nullptr);
  221.  
  222. auto p = 0; // makes auto as int
  223. auto p = nullptr; // makes auto as decltype(nullptr)
  224.  
  225. MyClass *arr[4];
  226. std::fill_n(arr, 4, NULL);
Add Comment
Please, Sign In to add comment