views:

151

answers:

3

In the following example:

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

    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 tones 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 usefull link text. I'm not alone :) Less strict checkings could be much more usefull.

+8  A: 

The fact that the parameter has a different type from the member function doesn't affect the fact that the parameter shadows the member function.

Why would you expect there not to be a warning about this?

James McNellis
I'm interesting in cases when variable hides variable. This potentially a bug. But case when variable "hides" a function is just not interesting.
dimba
@idimba: I disagree. A function pointer may be stored in a variable. Also a variable may have an `operator()` defined for it so it can be invoked like a function.
Troubadour
It may not be interesting, but it *is* shadowing. And the warning is called -Wshadow, not -Winteresting. So I'd expect it to warn when one name hides another, not when @idimba thinks the code is "interesting".
jalf
@jalf: I'd pay good money for a compiler that had a -Winteresting flag that only showed me warnings about which I was interested.
James McNellis
+3  A: 

It does exactly what it says on the box. It warns you of shadowing.

Inside the setLen function, two symbols are in scope which share the same name. len is the name of a function parameter, and also the name of a function.

One shadows the name of the other, so when you write code that refers to len, you may not get the result you wanted. Since you asked the compiler to warn you about symbols shadowing for each others, this is what it warns you about.

jalf
+2  A: 

I don't understand why you insist only on some specific types of shadowing. Shadowing is shadowing, and the dangers of it are the same even if the types are different and even if a variable shadows a function, as in your case. The danger of shadowing is that the code might do something different from what its author wanted it to do.

This, BTW, can easily happen when a variable is shadowing a function, since in C++ the distinction between the two is much thinner than it might seem at the first sight.

For example, here a variable shadows a function

struct L { 
  void operator ()(); 
};

struct A {
  void len();

  A(L len) {
    len();
    // Intended to call the member function. Instead got a call to the functor
  }
};

and I think it is pretty obvious that because of the shadowing the code might do something the author did not intend it to do.

AndreyT