opCmp with and without const

Paul Backus snarwin at gmail.com
Fri Dec 6 07:54:02 UTC 2019


On Friday, 6 December 2019 at 07:03:45 UTC, berni44 wrote:
> My real question is: Can this code duplication be avoided 
> somehow? (I ask, because I've got a PR running, which increases 
> the size of these functions and it doesn't feel good to have 
> two long, almost identical functions.)

You can use a template this parameter [1] to have a new copy of 
opCmp generated for each qualified version of Tuple it's called 
with:

int opCmp(R, this This)(R rhs) const
if (areCompatibleTuples!(typeof(this), R, "<"))
{
     ...
}

This may lead to binary bloat, though, since you can potentially 
have separate instantiations for mutable, const, immutable, 
inout, shared, etc.

[1] https://dlang.org/spec/template.html#template_this_parameter


More information about the Digitalmars-d-learn mailing list