isMutable doesn't work with enum ?

Jonathan M Davis jmdavisProg at gmx.com
Sun Jun 2 15:40:32 PDT 2013


On Monday, June 03, 2013 00:34:49 Temtaime wrote:
> It's intresting.
> So the only way to pass "a" to overloaded function like that ?
> 
> void foo(T)(ref T) { writeln("true"); }
> void foo(T)(auto ref in T) { writeln("false"); }
> 
> enum int a = 10;
> foo(a); // false
> 
> int b = 1;
> foo(b); // true

ref only accepts lvalues. An enum is not an lvalue. auto ref makes it so that 
if the templated function is passed an lvalue, it generates a ref version, 
whereas if it's passed an rvalue, it generates a non-ref version. It would 
appear that the compiler considers ref to be a better match than auto ref (as 
opposed to giving an error), so when you pass foo an lvalue, the ref version 
gets called, but when you pass it an rvalue, the only viable overload is the 
auto ref one, so it gets called.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list