Is returning void valid code?

后端 未结 4 1535
时光说笑
时光说笑 2020-12-01 13:29

I found out that the following code gets accepted by Visual C++ 2008 and GCC 4.3 compilers:

void foo()
{

}

void bar()
{
  return foo();
}

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 14:08

    This is valid and can be quite useful for example to create cleaner code in situations when you want to do some error handling before returning:

    void ErrRet(int code, char* msg)
    {
       // code logging/handling error
    }
    void f()
    {
       if (...) return ErrRet(5, "Error Message !");
       // code continue
    }
    

提交回复
热议问题