Issue 15615 - Creating a Variant with a const primitive type doesn't compile
Summary: Creating a Variant with a const primitive type doesn't compile
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2016-01-27 03:49 UTC by Alex Parrill
Modified: 2024-12-01 16:25 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Alex Parrill 2016-01-27 03:49:34 UTC
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 Dlang Bot 2020-03-22 19:21:06 UTC
@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 dlangBugzillaToGithub 2024-12-01 16:25:51 UTC
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