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.)