Bug 15615 – Creating a Variant with a const primitive type doesn't compile

Status
NEW
Severity
normal
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-01-27T03:49:34Z
Last change time
2024-12-01T16:25:51Z
Keywords
pull
Assigned to
No Owner
Creator
Alex Parrill
Moved to GitHub: phobos#10156 →

Comments

Comment #0 by initrd.gz — 2016-01-27T03:49:34Z
Trying to construct a variant with a constant value type (i.e. the value is copied so the const doesn't matter) fails with a compiler error. Example: import std.variant; alias Value = Algebraic!(long, double); void main() { const long foo = 123L; long bar = foo; Value baz = Value(foo); } > rdmd ~/test.d /usr/include/dmd/phobos/std/variant.d(544): Error: static assert "Cannot store a const(long) in a VariantN!(8LU, long, double)" /home/col/test.d(9): instantiated from here: __ctor!(const(long)) Failed: ["dmd", "-v", "-o-", "/home/col/test.d", "-I/home/col"] The same issue occurs if `foo` is immutable, or a double or boolean. However, it does work with structs or slices: import std.variant; struct MyStruct { int a, b; } alias Value = Algebraic!(MyStruct, string); void main() { const MyStruct foo = MyStruct(1, 2); MyStruct bar = foo; Value baz = Value(foo); const string foo2 = "asdf"; string bar2 = foo2; Value baz2 = Value(foo2); }
Comment #1 by dlang-bot — 2020-03-22T19:21:06Z
@apz28 created dlang/phobos pull request #7427 "Fix Issue 15615" fixing this issue: - Fix Issue 15615 https://github.com/dlang/phobos/pull/7427
Comment #2 by robert.schadek — 2024-12-01T16:25:51Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10156 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB