Annotating SortedRange.empty const: Best way to auto-deduce annotations?

Steven Schveighoffer schveiguy at gmail.com
Wed Sep 2 21:40:59 UTC 2020


On 9/2/20 5:23 PM, SimonN wrote:
> Hi,
> 
> About this issue in Phobos:
> https://issues.dlang.org/show_bug.cgi?id=21216
> SortedRange.empty should be const, .front should be inout
> 
> Just adding const/inout to SortedRange's methods won't be enough; if we 
> add const/inout here, then many other Phobos ranges need to become 
> const/inout-correct to keep the tests passing. Before I dive deeper into 
> annotating their methods, I would like to verify my assumptions on how 
> template function attribute deduction works:
> 
> 1) I know that templated functions deduce their own attributes on 
> instantiation. But front()/empty() are non-templated methods within the 
> templated struct SortedRange. Will attribute deduction happen here?

mutability attributes are not deducted for templates.

wrapping ranges such as SortedRange can't really specify const or inout 
on their methods via introspection of the underlying range. What they 
can do is template the `this` parameter. Then if the underlying range 
supports calling that way, it will work, otherwise it won't.

using `template this` should be compatible with the existing code I 
would think.

-Steve


More information about the Digitalmars-d-learn mailing list