inout and templates don't mix...

cy via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 23 14:31:17 PDT 2016


halp

There's a module that tries to define complex operations on both 
const and non-const structs, since it's the same operation for 
both. So every function that invokes those operations is 
copy-pasted twice, just with "const" added. Switching to inout to 
eliminate that huge amount of code duplication causes an error, I 
can't figure out how to fix.

struct Someop(Type) {
   Type thing;
   void foo() {
	thing.bar();
   }
}

struct Foo {
   void bar() {
	import std.stdio: writeln;
	writeln("bar");
   }
}

struct Bar {
   void thingy(inout(Foo) foo) inout {
	auto op = Someop(foo);
	op.foo();
   }
}

void main() {
   Foo foo;
   Bar bar;
   bar.thingy(foo);
}

=>

Error: struct derp.Someop cannot deduce function from argument 
types !()(inout(Foo))

if I put in Someop!(typeof(foo))(foo) it gives the error:

Error: variable derp.Someop!(inout(Foo)).Someop.thing only 
parameters or stack based variables can be inout

...even though Someop is a struct allocated on the stack.

What I'm dealing with is like:

struct Bar {
   void thingy(Foo foo) {
	auto op = Someop(foo);
	//...lotsastuff...
	op.foo();
   }
   void thingy(const(Foo) foo) const {
	auto op = Someop(foo);
	//...lotsastuff...
	op.foo();
   }
   // repeat ad-nauseum...
}


More information about the Digitalmars-d-learn mailing list