-Waggregate-return的目的/行为方面的混淆? [英] Confusion in regards to purpose/behavior of -Waggregate-return?

查看:465
本文介绍了-Waggregate-return的目的/行为方面的混淆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查看 GCC的警告选项时,我遇到了 -Waggregate-return


-Waggregate-return >
如果定义或调用了返回结构或联合的任何函数,则发出警告。 (在您可以返回数组的语言中,这也会引发警告。)







引发警告的小例子:

  class foo {}; 
foo f(void){return foo {};}
int main(){}




$ g ++ -std = c ++ 0x -Waggregate-return -o main main.cpp

main.cpp:在函数'foo f()' :

main.cpp:2:5:warning:函数返回一个聚集[-Waggregate-return]







另一个不会引发警告的小例子:

  #include< string> 
std :: string f(void){returntest;}
int main(){}






使用-Waggregate-return获得的收益是什么?

为什么有人会对此提出警告? >
另外,是不是std :: string一个类? - 为什么我没有在第二个示例中警告'返回的聚合'?

解决方案

遵循@AlokSave所做的评论,以下是对答案的后续编辑:

这是编译器的两种可能的解释旗。由于关于它的文档很少,所以它的含义有些不清楚,但主要有两种可能的解释:1)警告用户返回一个聚合对象使他意识到如果聚合对象(它被分配在堆栈上)返回,堆栈可能会溢出。



显然,一些旧的C编译器不支持返回聚集(你必须返回一个指针)。



这两个中哪一个最好,我很难判断。但是,有关此标志的更多相关信息可以在以下链接中找到:

http://bytes.com/topic/c/answers/644271-aggregate-return-warnings



https://lists.gnu.org /archive/html/bug-gnulib/2012-09/msg00006.html



从后面的链接引用:





在我熟悉的GNU应用程序(Emacs,coreutils,.. )
我们只需禁用-Waggregate-return。它完全是
的过时警告,因为它的动机是
支持向后兼容C编译器,
不允许返回结构。




这些编译器
已经很久没有实际问题了。 / blockquote>

While looking at the GCC's warning options, I came across -Waggregate-return.

-Waggregate-return
Warn if any functions that return structures or unions are defined or called. (In languages where you can return an array, this also elicits a warning.)


small example that elicits the warning:

class foo{};
foo f(void){return foo{};}
int main(){}

$ g++ -std=c++0x -Waggregate-return -o main main.cpp
main.cpp: In function ‘foo f()’:
main.cpp:2:5: warning: function returns an aggregate [-Waggregate-return]


another small example that does not elicit the warning:

#include <string>
std::string f(void){return "test";}
int main(){}


What is the benefit gained from using -Waggregate-return?
Why would someone want to be warned about this?
Also, isn't std::string a class?- why arn't I warned about the 'returned aggregate' in the second example?

解决方案

Following the comments made by @AlokSave, here is a later edit of the answer:

Three are two possible explanations for this compiler flag. Since the documentation about it is scarce, it is somewhat unclear what its original meaning is, but there are, mainly, two possible explanations:

1) Warning the user about returning an aggregate object make him aware that the stack could overflow if the aggregate object (which is allocated on the stack) is returned.

2) Apparently, some old C compiler did not support returning aggrregates (you had to return a pointer).

Which of the two is the best one, it is hard for me to judge. However, more relevant information about this flag may be found at the following links:

http://bytes.com/topic/c/answers/644271-aggregate-return-warnings

https://lists.gnu.org/archive/html/bug-gnulib/2012-09/msg00006.html

Quoting from the latter link:

In the GNU apps I'm familiar with (Emacs, coreutils, ...) we simply disable -Waggregate-return. It a completely anachronistic warning, since its motivation was to support backwards compatibility with C compilers that did not allow returning structures. Those compilers are long dead and are no longer of practical concern.

这篇关于-Waggregate-return的目的/行为方面的混淆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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