Returns NULL in a function that should return a vector

I have a function with the following heading:

std::vector<NxU32> MySewer::createCuttingArray(MyCloth** cloth, NxU32 startPoint, NxU32 endPoint) 

The function should return a vector containing some integer values. But there is one problem: if the string is incorrectly created (there is a way that this can happen), I want to do something like what I did in Java and C # to return NULL. But from what I see in C ++, NULL is defined as an integer value. How can I return a valid NULL for a vector?

+4
source share
7 answers

I do not think there is a need for boost::optional and std::exception . What you have to return is an empty vector. I'm thinking about it. The function returns a list of integers, and the caller function works in a list of integers. If the list is empty, then the operator does not work. For example (in psuedo)

 std::vector<T> data = createData(args); for(int i = 0; i < data.size(); ++i){ calculate(data[i]); } 

That the loop will not execute if the data is empty. No need for null checks or exception handling.

+1
source

The โ€œrightโ€ way to handle this really depends on what the code that receives the vector actually does. Is this the cause of the error, or is it just โ€œwe ignore itโ€?

If this is an error, use throw some_exception_goes_here; because itโ€™s a lot easier than going down a line with a NULL value.

If you just want to ignore it, return an empty vector, and then make sure that the code below will not have problems with an empty vector.

+11
source

According to your description, you want boost::optional<std::vector<NxU32>> .

Hope this name makes it obvious.

http://www.boost.org/doc/libs/release/libs/optional/doc/html/index.html

+7
source

boost::optional<T> solves this problem by extending the type T as a type that can either "have a value of type T " or "have no value". In your case, you return boost::optional<std::vector<NxU32>> , because in some cases you want to return "nothing."

The boost documentation has some examples here .

This functionality seems simple enough that you can expect it to become part of the standard library. Unfortunately, this is not all. See Also: boost :: additional alternative in the C ++ standard library

+1
source

You can not. To emulate the behavior of reference types in .NET, of which variables can either be bound to a real object or be null, you need to use pointers in C ++. Pointers can be either NULL or point to a valid object.

0
source

All in all, I think I would change the template. Create a vector in the caller, pass as a reference (or pointer) to your function and get bool return true / false, indicating success or failure.

Doing this also means that you are not calling the constructor std :: vector <> copy for the return value.

If you already have formatting and start and start installed, I will be tempted to return boost :: optional, as other answers indicate. But note that improving work efficiency can be a headache (it has its own build environment, which is a learning curve), especially if you use StlPort.

0
source

You just can't do it in C ++.

You are returning a vector to be copied (and created between other things). Another thing is that you cannot overlay something that is not a vector in NULL.

-2
source

Source: https://habr.com/ru/post/1483610/


All Articles