是否有用于隐式返回值的GCC扩展?
Is there a GCC extension for implict return values?
我有,并且已经纠正了,类似于下面的代码:
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
的地方得到了正确的返回代码。我可以想到两种解释:
在GCC中有一些扩展可以识别包装其他函数的内联函数并传递返回值。
返回代码正在传递,因为未定义行为的幸运/不幸表现与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中阻止任何错误或警告的错误。
我想你会发现事情就是这样的。
从技术上讲,这是未定义的行为。
相关文章:
- 如何在macOS中的旧扩展clang和gcc编译器中初始化数组和向量
- 带有 gcc 和 clang 的可变参数宏扩展的奇怪行为
- 为什么gcc会破坏递归扩展宏
- 在折叠表达式中扩展参数包作为 lambda 捕获的一部分 - gcc 与 clang
- GCC vs CLANG:将捕获的参数包扩展两次
- c++:gcc 是否提供扩展宏/函数,例如数组大小"countof"?
- gcc矢量扩展中未对齐的加载/存储
- 利用gcc和clang进行多参数包扩展
- 将参数包扩展到具有折叠表达式的lambda -GCC与Clang
- GCC 不接受默认模板参数中的包扩展
- GCC与Visual Studio宏观扩展
- 未记录的GCC扩展:结构中的VLA
- 为内存管理扩展GCC
- GCC 的扩展初始值设定项列表警告
- GCC 4.6 和缺少的可变模板扩展
- gcc 扩展或宏,用于在编译时检查用于某些基本类型的位
- 是否有用于 c++ 短路的 gcc 扩展
- 初始化带有大括号的VLA是GCC错误或扩展
- 作为GCC 4.9扩展的正则函数中的参数,是auto
- 是否有用于隐式返回值的GCC扩展?