Warning on template parameter identifier shadowing?

Steven Schveighoffer schveiguy at gmail.com
Wed Aug 5 17:54:47 UTC 2020


On 8/5/20 1:27 PM, Paul Backus wrote:
> On Wednesday, 5 August 2020 at 16:57:49 UTC, Steven Schveighoffer wrote:
>> In essence, this is no different than erroring on shadowing of 
>> function variables, which could also be allowed, but saves a lot of 
>> trouble. You can look at it more as a "hey D, you have this position 
>> on variable names, but not on template parameter names, why the 
>> inconsistency?"
> 
> The case you point out here is the exception, not the rule. D allows 
> shadowing for everything *except* local variables in functions. The most 
> consistent thing to do would be to allow it everywhere, period; 
> presumably an exception was made because that specific type of shadowing 
> was thought to be particularly error-prone.

One reason shadowing of variables is not allowed, but shadowing of 
module-level symbols is fine is because you can explicitly refer to the 
module-level symbols by prepending a dot.

I haven't thought completely through it, but if I could, I'd ban all 
shadowing of anything that could not be accessed via some explicit scope 
expression.

> 
> Note that the exception does not apply to *parameters* of nested 
> functions, which is the analogous case here:
> 
> void foo(int x) {
>      void bar(int x) {} // ok
> }

Yes you are right of course. I happen to believe it should require a 
difference here as well. Again, in my own code, I avoid this case.

I still believe that a) it's very error prone to define nested templates 
with the same parameter names as the parameters of the outer template, 
and b) the burden of requiring everything to have a unique name would be 
minimal.

I happen to do this anyway when I remember. I would appreciate the 
compiler helping me out when I don't. Hence, I'm in favor of such a 
proposal.

-Steve


More information about the Digitalmars-d mailing list