[Issue 2150] New: cannot get values from const variant
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Jun 14 08:08:55 PDT 2008
http://d.puremagic.com/issues/show_bug.cgi?id=2150
Summary: cannot get values from const variant
Product: D
Version: 2.014
Platform: PC
OS/Version: Windows
Status: NEW
Keywords: rejects-valid
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: andrei at metalanguage.com
ReportedBy: blprice61 at yahoo.com
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)...
--
More information about the Digitalmars-d-bugs
mailing list