GCC 隐藏/鲜为人知的功能
这是我尝试开始收集通常不会遇到的 GCC 特殊功能。这是在 @jlebedev 在另一个问题中提到 g++ 的“Effective C++”选项之后出现的,
-Weffc++ 此选项警告 C++ 代码违反了 Scott Meyers 所著的《Effective C++》和《More Effect C++》书中给出的一些编程准则。例如,如果使用动态分配内存的类没有定义复制构造函数和赋值运算符,则会发出警告。请注意,标准库头文件不遵循这些准则,因此您可能希望使用此选项偶尔测试您自己的代码中可能存在的问题,而不是始终使用它进行编译。
还有哪些其他很酷的功能?
This is my attempt to start a collection of GCC special features which usually do not encounter. this comes after @jlebedev in the another question mentioned "Effective C++" option for g++,
-Weffc++
This option warns about C++ code which breaks some of the programming guidelines given in the books "Effective C++" and "More Effective C++" by Scott Meyers. For example, a warning will be given if a class which uses dynamically allocated memory does not define a copy constructor and an assignment operator. Note that the standard library header files do not follow these guidelines, so you may wish to use this option as an occasional test for possible problems in your own code rather than compiling with it all the time.
What other cool features are there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我时不时地浏览当前的 GCC/G++ 命令行参数文档并更新我的编译器脚本,以便对任何类型的编码错误更加偏执。如果您有兴趣,这里就是。
不幸的是我没有记录它们,所以我忘记了大部分,但是 -pedantic、-Wall、-Wextra、-Weffc++、-Wshadow、-Wnon-virtual-dtor、-Wold-style-cast、-Woverloaded-virtual 和一些其他的总是有用的,警告我潜在的危险情况。我喜欢可定制性的这一方面,它迫使我编写干净、正确的代码。它对我很有帮助。
然而,它们并非没有令人头痛的问题,尤其是 -Weffc++。仅举几个例子:
Final
类(它阻止T 的子类化(如果 T 实际上从 T 派生)必须将 T 包装在私有包装类中以将其声明为友元,因为标准平面禁止与模板参数成为友元。这听起来可能有点受虐狂,但总的来说,这些都是非常酷的功能,增加了我对 C++ 和通用编程的理解。
G++ 还有哪些其他很酷的功能?嗯,它是免费的,开放的,它是使用最广泛和现代的编译器之一,始终优于竞争对手,几乎可以吃掉人们扔给它的任何东西,几乎可以在每个平台上使用,可定制到地狱,不断改进,拥有广泛的社区 -有什么理由不喜欢呢?
From time to time I go through the current GCC/G++ command line parameter documentation and update my compiler script to be even more paranoid about any kind of coding error. Here it is if you are interested.
Unfortunately I didn't document them so I forgot most, but -pedantic, -Wall, -Wextra, -Weffc++, -Wshadow, -Wnon-virtual-dtor, -Wold-style-cast, -Woverloaded-virtual, and a few others are always useful, warning me of potentially dangerous situations. I like this aspect of customizability, it forces me to write clean, correct code. It served me well.
However they are not without headaches, especially -Weffc++. Just a few examples:
Final<T>
class (which prevents subclassing of T if T derived from it virtually) had to wrap T in a private wrapper class to declare it as friend, since the standard flat out forbids befriending a template parameter.It might sound a bit masochist, but as a whole, these are very cool features that increased my understanding of C++ and general programming.
What other cool features G++ has? Well, it's free, open, it's one of the most widely used and modern compilers, consistently outperforms its competitors, can eat almost anything people throw at it, available on virtually every platform, customizable to hell, continuously improved, has a wide community - what's not to like?
如果遵循的代码路径在没有“返回值”语句的情况下结束函数,则返回值(例如 int)的函数将返回随机值。不注意这一点可能会导致异常和超出范围的内存写入或读取。
例如,如果使用函数来获取数组的索引,并且使用了错误的代码路径(不以 return 'value' 语句结尾的代码路径),则将返回一个可能太大的随机值作为数组的索引,当您错误地弄乱堆栈或堆时,会导致各种令人头痛的问题。
A function that returns a value (for example an int) will return a random value if a code path is followed that ends the function without a 'return value' statement. Not paying attention to this can result in exceptions and out of range memory writes or reads.
For example if a function is used to obtain the index into an array, and the faulty code path is used (the one that doesn't end with a return 'value' statement) then a random value will be returned which might be too big as an index into the array, resulting in all sorts of headaches as you wrongly mess up the stack or heap.