Variant and immutable struct

Vlad Leberstein via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 5 19:22:47 PST 2016


Hi!

I've stumbled into some strange problem. I'm trying to put an 
immutable struct into std.variant.Variant but get some 
compilation error(but only when struct has at least one member). 
Stripped down example(tested on Linux dmd64 v2.069.2):

import std.variant : Variant;


immutable struct Test {
	int member;	
}

int main() {
	Test v;
	Variant test = v;
	return 0;
}

Stacktrace:
/usr/include/dmd/phobos/std/variant.d(311,25): Error: cannot 
modify struct *zat Test with immutable members
/usr/include/dmd/phobos/std/variant.d(630,21): Error: template 
instance std.variant.VariantN!32LU.VariantN.handler!(Test) error 
instantiating
/usr/include/dmd/phobos/std/variant.d(546,17):        
instantiated from here: opAssign!(Test)
src/app.d(95,10):        instantiated from here: __ctor!(Test)

The same happens with immutable class(there is related thread 
with workaround at 
http://www.digitalmars.com/d/archives/digitalmars/D/learn/Sending_an_immutable_object_to_a_thread_73866.html), but not with immutable primitives(e.g. immutable int). Is this a bug or just my misunderstanding?

Thanks in advance!


More information about the Digitalmars-d-learn mailing list