Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class T>
- T foo(T a) {
- if (a) {
- // do somethin', returns object of type T
- } else {
- return NULL;
- }
- }
- template <class T>
- T* foo(T a) {
- if (a) {
- // do somethin', returns object of type T*
- } else {
- return nullptr;
- }
- }
- template <class T>
- std::optional<T> foo(T a) {
- if (a) {
- // do somethin', returns object of type T
- return std::make_optional(/*Anything that constructs `T`*/);
- } else {
- return {};
- }
- }
- auto my_val = foo(obj);
- if(my_val){
- /* :-) ....knock yourself out! */
- }
- else{
- /* :-( ....we didn't find the value */
- }
- if(auto my_val = foo(obj); my_val){
- // ....knock yourself out!
- }
- return listData[i];
- cout << "Does not exist";
- return {};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement