Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 按值返回-错误指示值_C++ - Fatal编程技术网

C++ 按值返回-错误指示值

C++ 按值返回-错误指示值,c++,C++,给出以下简单的函数声明: vector<Date> getMonthArray(int month, int year); vector getMonthArray(整数月,整数年); 对于C++ 11,我可以利用移动语义并按值返回日期向量,而不会丢失任何性能,避免使用“新”和“删除”。 问题是,如果函数失败,我不知道返回什么。。 例如,月份或年份小于零 我不能像指针那样返回null, 所以我考虑了两种可能性: 1) 返回一个空数组 2) 抛出异常 老实说,我两个都讨厌。希望能听

给出以下简单的函数声明:

vector<Date> getMonthArray(int month, int year);
vector getMonthArray(整数月,整数年);

对于C++ 11,我可以利用移动语义并按值返回日期向量,而不会丢失任何性能,避免使用“新”和“删除”。 问题是,如果函数失败,我不知道返回什么。。 例如,月份或年份小于零

我不能像指针那样返回null, 所以我考虑了两种可能性:

1) 返回一个空数组 2) 抛出异常

老实说,我两个都讨厌。希望能听到更多有经验的C++程序员更好的解决方案。 编辑:


因此,抛出异常看起来真的是最优雅的解决方案,因为它不涉及用户为了正确运行函数而需要阅读的任何文档(可以抛出的异常除外)。我说得对吗?或者还有其他选择吗?

如果可能失败,空向量不是一个糟糕的主意。人们似乎喜欢返回错误代码的一种方法是将向量作为参考参数,并将函数的返回值更改为布尔值,如果返回代码的类型更多(也可以是枚举类型),则将返回值更改为int。根据返回的值,向量可能具有不同的条件。我认为在这种情况下,尽管空向量是好的,只要它有很好的文档记录

参考参数示例,布尔返回方法

//pre: none
//post: if month and year are valid, monthArray will contain array 
//      of months and function returns true.
//      Otherwise returns false with no guarantees on contents of monthArray
bool getMonthArray(int month, int year, vector<Date>& monthArray);
//pre:none
//post:如果月份和年份有效,monthArray将包含数组
//函数返回true。
//否则返回false,不保证monthArray的内容
布尔-格蒙特哈雷(整数月、整数年、矢量和蒙特哈雷);
错误代码示例,参考参数

//pre: none
//post: if month and year are valid, monthArray will contain array 
//      of months and function returns 0 for success.
//      If month is valid but year is not, returns -1
//      If month is invalid, but year is not, returns -2
//      If month and year are valid, but otherwise failed, returns -3
int getMonthArray(int month, int year, vector<Date>& monthArray);
//pre:none
//post:如果月份和年份有效,monthArray将包含数组
//函数返回0表示成功。
//如果月份有效但年份无效,则返回-1
//如果月份无效,但年份无效,则返回-2
//如果月份和年份有效,但在其他方面失败,则返回-3
int getMonthArray(int月、int年、vector和monthArray);

如果它可能失败,空向量不是一个糟糕的主意。人们似乎喜欢返回错误代码的一种方法是将向量作为参考参数,并将函数的返回值更改为布尔值,如果返回代码的类型更多(也可以是枚举类型),则将返回值更改为int。根据返回的值,向量可能具有不同的条件。我认为在这种情况下,尽管空向量是好的,只要它有很好的文档记录

参考参数示例,布尔返回方法

//pre: none
//post: if month and year are valid, monthArray will contain array 
//      of months and function returns true.
//      Otherwise returns false with no guarantees on contents of monthArray
bool getMonthArray(int month, int year, vector<Date>& monthArray);
//pre:none
//post:如果月份和年份有效,monthArray将包含数组
//函数返回true。
//否则返回false,不保证monthArray的内容
布尔-格蒙特哈雷(整数月、整数年、矢量和蒙特哈雷);
错误代码示例,参考参数

//pre: none
//post: if month and year are valid, monthArray will contain array 
//      of months and function returns 0 for success.
//      If month is valid but year is not, returns -1
//      If month is invalid, but year is not, returns -2
//      If month and year are valid, but otherwise failed, returns -3
int getMonthArray(int month, int year, vector<Date>& monthArray);
//pre:none
//post:如果月份和年份有效,monthArray将包含数组
//函数返回0表示成功。
//如果月份有效但年份无效,则返回-1
//如果月份无效,但年份无效,则返回-2
//如果月份和年份有效,但在其他方面失败,则返回-3
int getMonthArray(int月、int年、vector和monthArray);

简单,传递向量并返回一个
bool
以获得成功

bool getMonthArray(std::vector<Date>* out, int month, int year);
bool getMonthArray(标准::向量*out,整数月,整数年);

在您的实现中,如果参数无效,则返回false。

简单,传递向量并返回一个
bool
以获得成功

bool getMonthArray(std::vector<Date>* out, int month, int year);
bool getMonthArray(标准::向量*out,整数月,整数年);

在您的实现中,如果参数无效,则返回false。

选项很少:

  • 返回指示错误的正常返回类型的特殊值
  • 将返回更改为错误代码(或标志)并具有输出参数(引用或指针)
  • 返回包含值和错误指示器(例如)的包装
  • 抛出异常
  • 具有不能无效的参数 选项1到4非常明显

    选项5并不总是可行的,但在某些情况下,仔细选择参数类型(包括枚举)和解释是可行的

    不知道您的功能是什么,以下是一些建议:

    a。将月份和年份的类型更改为未签名。
    B将“月”解释为连续的:

    vector<Date> getMonthArray(unsigned int month, unsigned int year) {
       year = year + month / 12;
       month = month % 12;
       ...
    
    vector getMonthArray(无符号整数月,无符号整数年){
    年=年+月/12;
    月份=月份%12;
    ...
    
    选项非常少:

  • 返回指示错误的正常返回类型的特殊值
  • 将返回更改为错误代码(或标志)并具有输出参数(引用或指针)
  • 返回包含值和错误指示器(例如)的包装
  • 抛出异常
  • 具有不能无效的参数
  • 选项1到4非常明显

    选项5并不总是可行的,但在某些情况下,仔细选择参数类型(包括枚举)和解释是可行的

    不知道您的功能是什么,以下是一些建议:

    a、 将月份和年份的类型更改为未签名。
    b、 将“月”解释为连续的:

    vector<Date> getMonthArray(unsigned int month, unsigned int year) {
       year = year + month / 12;
       month = month % 12;
       ...
    
    vector getMonthArray(无符号整数月,无符号整数年){
    年=年+月/12;
    月份=月份%12;
    ...
    
    在这里抛出异常是一个好办法。@rightfold:想详细说明一下吗?在这种情况下,你怎么可能知道OP的“正确”解决方案是什么?异常处理可能是一个棘手的问题,然后会有各种各样的问题,关于在哪里检查异常以及如何准确地检查异常