Confusion in regards to purpose/behavior of -Waggregate-return?(对 -Waggregate-return 的目的/行为感到困惑?)
问题描述
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 的目的/行为感到困惑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:对 -Waggregate-return 的目的/行为感到困惑?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
