class template conflict

Michelle Long HappyDance321 at gmail.com
Tue Dec 25 13:03:13 UTC 2018


On Monday, 24 December 2018 at 22:55:55 UTC, Daniel Kozak wrote:
> ne 23. 12. 2018 13:10 odesílatel Michelle Long via 
> Digitalmars-d-learn < digitalmars-d-learn at puremagic.com> napsal:
>
>> class X
>> {
>>
>> }
>>
>> class X(int N) : X
>> {
>>
>> }
>>
>> Is there any real reason we can't do this?
>
> Actually yes.  It would break almost all of my code.

How would it break your code?

>
> In D you can do thing like this:
> class X(int N)
> {
> X something; // it is same as X!N something;
> }
>
> So I do not need to write X!N everywhere inside X class template

But I am not talking about inside the template being used. The 
whole point of doing this is so that one can refer to the base 
class using the same name as the derived with a template 
parameter to make a composite structure.

X!N should be totally different than X.

The fact that you can use X inside a class to refer to X!N is a 
hack... and in any case should not effect what I'm talking about 
because it is only used in the inherited part.. which it would be 
circular to use it as it is:

class X(int N) : X
{

}

creates circular inheritance.

so for the inherited part it should never be used and you never 
use it in your code like that... so it won't break anything.

Also, as long as there is no other symbol with that name it won't 
break anything.

Suppose this did work:

class X;

class X(int N) : X // (Here X refers to the base class above
{
   // Using X can still be X!N since we can just alias the 
original X away.
}

class X;
alias XX = X;
class X(int N) : X
{
    X x; // X!N x;
    XX xx; // X = x;
}

So it would not break anything. It really has nothing to do with 
what one does inside a template but how it looks to the outside.




More information about the Digitalmars-d-learn mailing list