在函数中返回NULL,返回一个向量

时间:2022-03-22 04:22:23

I have a function with the following header:

我有一个带有以下标题的函数:

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

The function is supposed to return a vector containing some integer values. But there is one problem: If the line is not correctly created (there is a way in which this can happen), I want to do something like what I did in Java and C#, to return a 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?

该函数应该返回包含一些整数值的向量。但是有一个问题:如果没有正确创建行(有一种方法可以发生这种情况),我想做一些类似于我在Java和C#中所做的事情,以返回NULL。但是从我看来,在C ++中,NULL被定义为整数值。如何为向量返回有效的NULL?

7 个解决方案

#1


1  

I don't think there is a need for any boost::optional nor std::exception. What you should return is an empty vector. Think about it. The function returns a list of integers and the function caller operates on the list of integers. If the list is empty, then there is nothing to operator on. For example( in psuedo)

我认为不需要任何boost :: optional或std :: exception。你应该返回的是一个空的向量。想一想。该函数返回一个整数列表,函数调用者在整数列表上运行。如果列表为空,那么操作员就没有任何内容。例如(在psuedo中)

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

That for loop isn't going to execute if the data is empty. There is no need for null checks or exception handling.

如果数据为空,则不会执行for循环。不需要空检查或异常处理。

#2


11  

The "correct" way to deal with this really depends on what the code receiving the vector actually does. Is it cause for an error, or simply "we ignore this"?

处理这个问题的“正确”方法实际上取决于接收向量的代码实际上做了什么。是导致错误,还是只是“我们忽略了这个”?

If it's an error, use throw some_exception_goes_here;, because that is much easier than going down the line with a NULL value.

如果这是一个错误,请使用throw some_exception_goes_here;,因为这比使用NULL值下线容易得多。

If you want to just ignore it, return an empty vector and then make sure the code below isn't going to have problems with an empty vector.

如果你想忽略它,返回一个空向量,然后确保下面的代码不会出现空向量的问题。

#3


7  

By your description, what you want is boost::optional<std::vector<NxU32>>.

根据你的描述,你想要的是boost :: optional >。

I hope the name makes its intent obvious.

我希望这个名字的意图明显。

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

#4


1  

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

boost :: optional 通过将类型T扩展为可以“具有类型T的值”或“没有值”的类型来解决此问题。在你的情况下,你将返回一个boost :: optional >,因为你想在某些情况下返回“什么都没有”。

The boost documentation has some examples here.

boost文档在这里有一些例子。

This functionality seems basic enough that you might expect it to be part of the standard library. Sadly, it isn't (yet). See related: boost::optional alternative in C++ Standard Library

此功能似乎足够基本,您可能希望它成为标准库的一部分。可悲的是,它还没有。请参阅C ++标准库中的相关:boost :: optional替代方法

#5


0  

You can't. To emulate the behaviour of reference types in .NET, of which variables can be either attached to an actual object or be null, you need to use pointers in C++. Pointers can be either NULL or point to a valid object.

你不能。要模拟.NET中引用类型的行为,其中的变量可以附加到实际对象或为null,您需要在C ++中使用指针。指针可以是NULL或指向有效对象。

#6


0  

On balance, I think I'd change the pattern. Create the vector in the caller, pass as a reference (or pointer) into your function and have a bool return true / false indicating success or failure.

总的来说,我想我会改变模式。在调用者中创建向量,作为引用(或指针)传递到函数中,并使bool返回true / false表示成功或失败。

Doing it this way also means you're not invoking the std::vector<> copy constructor on the return value.

这样做也意味着你没有在返回值上调用std :: vector <>复制构造函数。

If you already have boost installed and up and running I'd be tempted to return boost::optional as other answers indicate. But note that getting boost to work can be a headache, (it has its own build environment which is a learning curve) especially if you're using StlPort.

如果你已经安装了升级并启动并运行,我很想回复boost :: optional,因为其他答案表明。但请注意,提升工作可能会令人头疼(它有自己的构建环境,这是一个学习曲线),特别是如果你正在使用StlPort。

#7


-2  

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

你不能用C ++做到这一点。

You are returning a vector that will be copied (and instantiated among other things). Other thing is that you can't cast something that isn't a vector to NULL.

您将返回一个将被复制(并在其他事物中实例化)的向量。另一件事是你不能将不是向量的东西转换为NULL。

#1


1  

I don't think there is a need for any boost::optional nor std::exception. What you should return is an empty vector. Think about it. The function returns a list of integers and the function caller operates on the list of integers. If the list is empty, then there is nothing to operator on. For example( in psuedo)

我认为不需要任何boost :: optional或std :: exception。你应该返回的是一个空的向量。想一想。该函数返回一个整数列表,函数调用者在整数列表上运行。如果列表为空,那么操作员就没有任何内容。例如(在psuedo中)

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

That for loop isn't going to execute if the data is empty. There is no need for null checks or exception handling.

如果数据为空,则不会执行for循环。不需要空检查或异常处理。

#2


11  

The "correct" way to deal with this really depends on what the code receiving the vector actually does. Is it cause for an error, or simply "we ignore this"?

处理这个问题的“正确”方法实际上取决于接收向量的代码实际上做了什么。是导致错误,还是只是“我们忽略了这个”?

If it's an error, use throw some_exception_goes_here;, because that is much easier than going down the line with a NULL value.

如果这是一个错误,请使用throw some_exception_goes_here;,因为这比使用NULL值下线容易得多。

If you want to just ignore it, return an empty vector and then make sure the code below isn't going to have problems with an empty vector.

如果你想忽略它,返回一个空向量,然后确保下面的代码不会出现空向量的问题。

#3


7  

By your description, what you want is boost::optional<std::vector<NxU32>>.

根据你的描述,你想要的是boost :: optional >。

I hope the name makes its intent obvious.

我希望这个名字的意图明显。

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

#4


1  

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

boost :: optional 通过将类型T扩展为可以“具有类型T的值”或“没有值”的类型来解决此问题。在你的情况下,你将返回一个boost :: optional >,因为你想在某些情况下返回“什么都没有”。

The boost documentation has some examples here.

boost文档在这里有一些例子。

This functionality seems basic enough that you might expect it to be part of the standard library. Sadly, it isn't (yet). See related: boost::optional alternative in C++ Standard Library

此功能似乎足够基本,您可能希望它成为标准库的一部分。可悲的是,它还没有。请参阅C ++标准库中的相关:boost :: optional替代方法

#5


0  

You can't. To emulate the behaviour of reference types in .NET, of which variables can be either attached to an actual object or be null, you need to use pointers in C++. Pointers can be either NULL or point to a valid object.

你不能。要模拟.NET中引用类型的行为,其中的变量可以附加到实际对象或为null,您需要在C ++中使用指针。指针可以是NULL或指向有效对象。

#6


0  

On balance, I think I'd change the pattern. Create the vector in the caller, pass as a reference (or pointer) into your function and have a bool return true / false indicating success or failure.

总的来说,我想我会改变模式。在调用者中创建向量,作为引用(或指针)传递到函数中,并使bool返回true / false表示成功或失败。

Doing it this way also means you're not invoking the std::vector<> copy constructor on the return value.

这样做也意味着你没有在返回值上调用std :: vector <>复制构造函数。

If you already have boost installed and up and running I'd be tempted to return boost::optional as other answers indicate. But note that getting boost to work can be a headache, (it has its own build environment which is a learning curve) especially if you're using StlPort.

如果你已经安装了升级并启动并运行,我很想回复boost :: optional,因为其他答案表明。但请注意,提升工作可能会令人头疼(它有自己的构建环境,这是一个学习曲线),特别是如果你正在使用StlPort。

#7


-2  

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

你不能用C ++做到这一点。

You are returning a vector that will be copied (and instantiated among other things). Other thing is that you can't cast something that isn't a vector to NULL.

您将返回一个将被复制(并在其他事物中实例化)的向量。另一件事是你不能将不是向量的东西转换为NULL。