const types can't be specialized non-const, if arrays?

cy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jun 16 12:43:03 PDT 2016


I don't get it. Do I have to write a separate template for arrays 
specifically or something?

NonConst foo(Constant: const NonConst, NonConst)(Constant bar) {
	pragma(msg,"NonConst is ",NonConst);
	pragma(msg,"Constant is ",Constant);
	NonConst foo = bar;
	return foo;
}

void main() {
	const int bar = 42;
	auto baz = foo(bar);
	pragma(msg,typeof(baz));
	baz = 23;
	const(int[]) barr = [1,2,3];
	auto bazz = foo(barr);
	pragma(msg,typeof(bazz));
	bazz[0] = 4;

}

/*

NonConst is int
Constant is const(int)
int
NonConst is const(int)[]
Constant is const(int)[]
const(int)[]
derp.d(16): Error: cannot modify const expression bazz[0]
Failed: ["dmd", "-v", "-o-", "derp.d", "-I."]

*/



More information about the Digitalmars-d-learn mailing list