`this` template params for struct not expressing constness.

Simen Kjærås simen.kjaras at gmail.com
Mon Jun 8 08:10:19 UTC 2020


On Monday, 8 June 2020 at 07:35:12 UTC, adnan338 wrote:
>     Self* searchTree(this Self)(auto in ref T item) const {
>         if (&this is null)
>             return null;
>         if (this.item == item)
>             return &this;
>         return (this.item < item) ?
>             this.right.searchTree(item) :
>             this.right.searchTree(item);
>     }

This method is const, which means 'this' is const, while Self is 
not. What you're looking for here is inout 
(https://dlang.org/spec/function.html#inout-functions):

     auto searchTree()(auto in ref T item) inout {
         if (&this is null)
             return null;
         if (this.item == item)
             return &this;
         return (this.item < item) ?
             this.right.searchTree(item) :
             this.right.searchTree(item);
     }

--
   Simen


More information about the Digitalmars-d-learn mailing list