gcc -Wshadow是否太严格? [英] gcc -Wshadow is too strict?

查看:825
本文介绍了gcc -Wshadow是否太严格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中:

  class A 
{
public:
int LEN();
void setLen(int len){len_ = len; } //在这一行发出警告
private:
int len_;
};

gcc with -Wshadow发出警告:

  main.cpp:4:警告:`len'声明会影响`this'的成员

函数len和integer len是不同类型的。为什么警告?



更新



我看到有一个广泛的cocensuse影子的意思。
从正式的角度来看,编译器的确如此。



然而恕我直言,这个标志并不实用。例如常用的setter / getter习语:

  class A {
void prop(int prop); // setter
int prop()const; // getter

int prop;
};

如果会有一个警告标志不会发出警告,
,但会在int a隐藏int a时发出警告。



为我的遗留代码添加-Wshadow发布大量警告,而不时发布
我发现由隐藏问题导致的错误。



我不介意如何将它称为-Wmuch_more_practical_and_interesting_shadow或
-Wfoooooo。



那么,是否有其他 gcc警告标志符合我所描述的内容?

更新2



不仅我认为-Wshadow某种程度上没用,链接文本 。我并不孤单:)
不太严格的检查可能更有用。

解决方案

这似乎已经解决了在新版本的GCC上。



从版本4.8
更新日志


 选项-Wshadow不再警告是否声明会影响函数声明,
,除非前者声明函数或指向函数的指针,因为这是一个常见的
和真实代码中的有效情况。

它引用了Linus Torvalds关于这个主题的想法:

不幸的是,我目前工作的嵌入式系统的最新编译器仍然基于gcc 4.6。


In the following example:

class A
{
  public:
    int len();
    void setLen(int len) { len_ = len; } // warning at this line
  private:
    int len_;
};

gcc with -Wshadow issue a warning:

main.cpp:4: warning: declaration of `len' shadows a member of `this'

function len and integer len are of different type. Why the warning?

Update

I see there's a wide cocensuse what "shadow" means. And from formal point of view the compiler do exactly what it meant too.

However IMHO, the flag is not practical. For example commonly used setter/getter idiom :

class A {
   void prop(int prop);  // setter
   int prop() const;     // getter

   int prop;
};

It would be nice if there will be a warning flag that won't issue warning in the case, but will warn in case "int a" hides "int a".

Adding -Wshadow on my legacy code issue tons of warnings, while from time to time I discover bugs caused by "shadowing" problem.

I don't mind how it will be called "-Wmuch_more_practical_and_interesting_shadow" or "-Wfoooooo".

So, is there're other gcc warning flag that does what I described?

Update 2

Not only me thinks -Wshadow somehow not useful link text. I'm not alone :) Less strict checkings could be much more useful.

解决方案

This seems to be solved on newer versions of GCC.

From version 4.8 changelog:

The option -Wshadow no longer warns if a declaration shadows a function declaration,
unless the former declares a function or pointer to function, because this is a common
and valid case in real-world code.

And it references Linus Torvalds's thoughts on the subject: https://lkml.org/lkml/2006/11/28/253

Unfortunately the newest compiler of the embedded system where I'm currently working is still based on gcc 4.6.

这篇关于gcc -Wshadow是否太严格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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