Bug 2150 – cannot get values from const variant

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2008-06-14T10:08:00Z
Last change time
2015-06-09T01:21:34Z
Keywords
rejects-valid
Assigned to
andrei
Creator
blprice61

Comments

Comment #0 by blprice61 — 2008-06-14T10:08:54Z
Unable to retrieve a value via get or coerce from a const variant. Also a related problem, unable to create a mutable copy of a const variant. The following code illustrates the issue: class Wrapper { private: Variant m_value; public: version(test_one) { this(const Variant value) { // m_value = value; // fails with same error as below m_value = Variant(value); // Error message: static assert "Cannot store a const(VariantN!(maxSize)) in a // VariantN!(maxSize)" } } else version(test_two) { this(Variant value) { m_value = value; } } int getInt() const { // return m_value.get!(int)(); // fails with same error as below return m_value.get!(const int)(); // Error message: this.m_value.get can only be called on a mutable object, // not const(VariantN!(maxSize)) } } unittest { int expected = 5; auto v = Variant(expected); Wrapper w = new Wrapper(v); int result = w.getInt(); assert(expected == result); } Attempts to cast away the const'ness of the variant and develop a workaround have failed with errors such as: cannot convert const(int) to const(int)...
Comment #1 by andrei — 2009-08-27T13:08:53Z
Fixed in the next release. (I'm still unhappy about a few details about Variant and const.)