Best practices for logical const
Meta
jared771 at gmail.com
Sat Feb 15 21:14:07 PST 2014
On Sunday, 16 February 2014 at 04:46:34 UTC, Steven Schveighoffer
wrote:
> It is safe if you can guarantee the input is ultimately
> mutable. But there is no way to enforce such a guarantee. It is
> on the caller to make sure that is true.
>
> I don't see why inout should be identified as any different
> from const in this respect. You could just as easily say the
> same thing about const.
>
> -Steve
Which is what I said in an earlier post. Wouldn't you be able to
do this with a template somehow?
template LogicalConst(T)
if (isMutable!T)
{
alias LogicalConst = inout(T);
}
void bumpWithConstArg(T)(ref LogicalConst!T t)
{
//Compiler error if you try to modify t
//t += 1;
//Fine, cast it to mutable then modify
//Nothing unsafe here because we verified t is mutable
cast(T)t += 1;
}
void main()
{
immutable n = 0;
//No IFTI match because n is immutable
bumpWithConstArg(n);
auto m = 0;
//Okay, m is mutable
bumpWithConstArg(m);
}
This doesn't actually work, but this is just off the top of my
head.
More information about the Digitalmars-d
mailing list