13

I have some code that looks like this:

template <class T>
T foo(T a) {
 if (a) {
   // do somethin', returns object of type T
 } else {
   return NULL;
 }
}

But of course it won't compile since NULL is not of type T. Someone suggested this solution to me but I don't like it:

template <class T>
T* foo(T a) {
 if (a) {
   // do somethin', returns object of type T*
 } else {
   return nullptr;
 }
}

I am wondering how to make this function able to return a NULL value if possible without the use of a pointer?

6
  • What do you return when you don't return null? Commented Aug 14, 2016 at 1:08
  • ^ an object of type T Commented Aug 14, 2016 at 1:09
  • Is this specific to templates? Commented Aug 14, 2016 at 1:11
  • 7
    I think what you're looking for is std::optional<T>. It's coming in C++17, but boost has one available right now. You can also find other implementations online. Commented Aug 14, 2016 at 1:11
  • Perhaps instead of trying to return a NULL-type value, you'd be better off throwing an exception? (if the NULL is meant to represent an error condition, anyway). Alternatively, instead of returning a value you could return a success/failure code, and write to a caller-supplied (T &) argument only in the success case. Commented Aug 14, 2016 at 1:38

2 Answers 2

15

In C++17, you will be able to use std::optional<T>. And you could do something like this:

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 {};
    }
}

And on the receiving end, you can test for the value being there:

auto my_val = foo(obj);
if(my_val){
     /* :-) ....knock yourself out! */
}
else{
     /* :-( ....we didn't find the value */
}

For now,

  • You can use Boost.Optional.

  • Or, if you are using a very recent compiler, you may be able to access it from std::experimental::optional.

  • Or, if you do not want to use Boost and its dependencies, you can simply grab this tiny header (a working implementation of optional from one of the proposers of optional into the C++ standard)... It's header only, so, you only need to download/copy that single header file and #include it.


Another cool thing with C++17 is that testing for the value will now be as simple as:

if(auto my_val = foo(obj); my_val){
     // ....knock yourself out!
}

You can see more of C++17 features here: What are the new features in C++17?

Sign up to request clarification or add additional context in comments.

5 Comments

Hi, how do I use c++17? That's exactly what I am looking for, thanks for your response.
Actually nevermind that was a silly question, I'll look into boost, thanks
@Liz, if you do not want to use Boost, you can simply grab this tiny header (a working implementation of optional)... It's header only, so, you only need to download/copy that single header file and #include it.
The new if isn't necessary for testing an optional, you can just write if (auto opt = foo(obj)) and it will do the right thing. thanks to optional's boolean conversion operator. The new syntax is only needed when the condition is more complex than "convert the declared variable to bool".
@SebastianRedl, Thanks for pointing that out!. I do that a lot too; I chose to use the new if because I was additionally trying to advertise C++17. (as you can also see in the very last line of my answer).
-3
template <class T>

T list<T>::getData(int i){

    if (i < iSize && i >= 0){

        return listData[i];

    } else {

        cout << "Does not exist";

    return {};

  }

}

//it works pretty well

2 Comments

I wouldn't call it 'null'. You return a default-constucted T.
How do you distinguish between the default-constructed result, and a listData[i] that ha the same value? (hint: you can't)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.