const Propagation

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Dec 29 06:18:41 PST 2014


On 12/29/14 8:20 AM, Julian Kranz wrote:
> Thank you for your answer. This kind of thing also works for C++, but
> that would mean that I would implement the whole visitor twice - one
> const and one non-const version. Is that really the only way? Can't I
> tell the D compiler that "the argument of that lambda shares the
> const-ness of the current object"?
>
> D offers "inout"; this actually aims into the right directing, but I
> guess it does not help here.

inout is not smart enough to know what the inout on a lambda means. It 
also can be confusing to understand that during the entire execution of 
an inout function, the inout parameters are treated as const. This would 
preclude using inout to do what you want.

> Is there any "static if"-something construct to check the const-ness of
> an object?

Static-if applies to generic coding, i.e. templates. A template function 
may work, but I don't think you even need static if:

class Hugo { ... }

void blah(T)(T obj, void function(T t) f) if(T : Hugo) { f(obj);}

This should work with all iterations of Hugo, and is callable via UFCS:

hugo.blah(f); // should work

I don't know if a template member function would be possible, as I'm 
pretty sure templates do not infer const.

(admittedly, I have not tested any of this)

-Steve


More information about the Digitalmars-d mailing list