Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. template <class T>
  2. T foo(T a) {
  3. if (a) {
  4. // do somethin', returns object of type T
  5. } else {
  6. return NULL;
  7. }
  8. }
  9.  
  10. template <class T>
  11. T* foo(T a) {
  12. if (a) {
  13. // do somethin', returns object of type T*
  14. } else {
  15. return nullptr;
  16. }
  17. }
  18.  
  19. template <class T>
  20. std::optional<T> foo(T a) {
  21. if (a) {
  22. // do somethin', returns object of type T
  23. return std::make_optional(/*Anything that constructs `T`*/);
  24. } else {
  25. return {};
  26. }
  27. }
  28.  
  29. auto my_val = foo(obj);
  30. if(my_val){
  31. /* :-) ....knock yourself out! */
  32. }
  33. else{
  34. /* :-( ....we didn't find the value */
  35. }
  36.  
  37. if(auto my_val = foo(obj); my_val){
  38. // ....knock yourself out!
  39. }
  40.  
  41. return listData[i];
  42.  
  43. cout << "Does not exist";
  44.  
  45. return {};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement