是否有用于隐式返回值的GCC扩展?

Is there a GCC extension for implict return values?

本文关键字:GCC 扩展 返回值 用于 是否      更新时间:2023-10-16

我有,并且已经纠正了,类似于下面的代码:

class SomeClass {
public:
    static int AdjustValue(float input);
    static int DoSomethingWithAdjustedValue(int adjustedInput);
    static int DoSomethingWithNormalValue(float input) {
        DoSomethingWithAdjustedValue(AdjustValue(input);}
};

所以DoSomethingWithNormalValue显然缺少return,但是GCC没有为它生成错误或警告。我调用DoSomethingWithNormalValue的地方得到了正确的返回代码。我可以想到两种解释:

  1. 在GCC中有一些扩展可以识别包装其他函数的内联函数并传递返回值。

  2. 返回代码正在传递,因为未定义行为的幸运/不幸表现与GCC中的一个错误相结合,该错误阻止了任何错误或警告。

我觉得这两种情况都不太可能。这是怎么回事?

c++不需要对此进行诊断,但这是未定义的行为。

您获得正确值的原因是因为DoSomethingWithAdjustedValue将其返回值放置在返回值所在的位置,然后DoSomethingWithNormalValue不会与任何返回值混淆,因此在调用DoSomethingWithNormalValue后寻找返回值的任何东西都会看到DoSomethingWithAdjustedValue返回的值。

try:

> g++ xa1.cpp -Wall
xa1.cpp: In static member function ‘static int SomeClass::DoSomethingWithNormalValue(float)’:
xa1.cpp:8: warning: no return statement in function returning non-void
xa1.cpp:8: warning: control reaches end of non-void function

返回代码被传递是因为未定义行为的幸运/不幸表现,以及GCC中阻止任何错误或警告的错误。

我想你会发现事情就是这样的。
从技术上讲,这是未定义的行为。