如何“如果当前语句出错,则退出当前功能".在C ++中 [英] How to "exit current function, if error on current statement" in C++

查看:55
本文介绍了如何“如果当前语句出错,则退出当前功能".在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个例子开始阐述我的问题.并在最后给出问题的确切说明.

I start with an example to elaborate my problem. And conclude with the exact statement of the question at the end.

因此,在C,我们可以写这样一个宏,

So in C, we can write a macro like this,

#define NO_ERROR 0
#define RETURN_IF_ERROR(function)                                       \
{                                                                       \
  RetCode = function;                                                   \
  if (RetCode != NO_ERROR)                                              \
  {                                                                     \
      LOG(ERROR,"[%d] line [%d] [%s]", RetCode, __LINE__, __FILE__ ) ); \
      return RetCode;                                                   \
  }                                                                     \
  else {                                                                \
      LOG(VERBOSE,"[%d] line [%d] [%s]", RetCode, __LINE__, __FILE__ ) );\
  }                                                                     \
}

现在,此宏可以在这样的函数中使用

Now this macro could be used in a function like this,

int ConstructAxes() {
    RETURN_IF_ERROR(GetAxis("alpha"));
    RETURN_IF_ERROR(GetAxis("beta"));
    RETURN_IF_ERROR(GetAxis("gamma"));
    RETURN_IF_ERROR(GetAxis("delta"));
    .
    .
    .
}

因此,如果其中调用的函数之一返回错误,则我们立即以错误代码退出当前函数(例如ConstructAxes).如果没有该宏,则对于这4条语句中的每条语句,我都必须编写一个if ... else块.我会最终用4行代码示出了实际功能或任务,然后错误校验(和/或任选的测井)的另一种16行. (我看到的功能有700多行长,只有20〜30行功能,还有600多行if ... else错误检查和记录.那么遵循主要逻辑并不是很容易.)

So, we exit the current function (e.g. ConstructAxes) immediately with an error code, if one of the functions called within, returns with an error. Without that macro, for each of those 4 statements, I would have to write an if...else block. I'd end up with 4 lines of code that shows the actual functionality or task and then another 16 lines of error-checking (and/or optional logging). (I have seen functions which are 700+ lines long with only 20~30 lines of functionality and 600+ lines of if...else error checking and logging. Not very easy to follow the main logic then.)

(ps,在任何人指出之前,我不能使用异常.这是一个遗留项目,并且不使用也不希望使用异常,我也不是编写异常安全代码的专家.同样在任何人指出之前,返回的错误代码被重新解释成一些有意义的文本,在一个较高的水平.在哪里,怎么不相关手头这个问题.)

(p.s. before anyone points out, I cannot use exceptions. This is a legacy project and exceptions are not used and not desired to be used, nor am I an expert at writing exception-safe code. Also before anyone points out, the returned error code is reinterpreted into some meaningful text, at a higher level. Where and how is not relevant to this question at hand.)

问题是,宏可能会出现问题,我更喜欢一个函数.因此,在C ++中有没有使用宏的简洁方法?我觉得这是不可能的.

The question is, macros can be problematic and I'd prefer a function. So is there some clean&elegant way to do this in C++ without using a macro? I have a feeling it is NOT possible.

推荐答案

您可能会编写一个循环,例如:

You might write a loop, something like:

int RunFunctions(int line, // __LINE__
                 const char* file, // __FILE__
                 std::initializer_list<std::function<int()>> functions)
{
    int counter = 0;
    for (auto f : functions) {
        auto RetCode = f();

        if (RetCode != NO_ERROR) {
            LOG(ERROR,"[%d] line [%d] [%s] [%d]", RetCode, line, file, counter ));
            return RetCode;
        } else {
            LOG(VERBOSE,"[%d] line [%d] [%s] [%d]", RetCode, line, file, counter ) );
        }
        ++counter;
    }
    return NO_ERROR;
}

int ConstructAxes() {
    return RunFunctions(__LINE__, __FILE__, {
        []() { return GetAxis("alpha"); },
        []() { return GetAxis("beta"); },
        []() { return GetAxis("gamma"); },
        []() { return GetAxis("delta"); }
    });
}

直到我们拥有 std :: source_location 或类似的产品,__LINE__ ,则需要__FILE__.

Until we have std::source_location or similar, __LINE__, __FILE__ would be required.

如果不需要捕获,则可以通过函数指针更改std::function.

If you don't need capture, you may change std::function by function pointers.

这篇关于如何“如果当前语句出错,则退出当前功能".在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆