1268 – Struct literals try to initialize static arrays of non-static structs incorrectly

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1268 - Struct literals try to initialize static arrays of non-static structs incorrectly
Summary: Struct literals try to initialize static arrays of non-static structs incorre...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P3 normal
Assignee: Walter Bright
URL:
Keywords: diagnostic, rejects-valid
Depends on:
Blocks:
Reported: 2007年06月15日 11:33 UTC by Matti Niemenmaa
Modified: 2014年02月16日 15:23 UTC (History)
0 users

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Matti Niemenmaa 2007年06月15日 11:33:59 UTC
struct S {
	int[5] x;
}
void main() {
	S s = S();
}
The above code fails to compile with the following errors:
Error: cannot implicitly convert expression (0) of type int to int[5]
Error: cannot cast int to int[5]
(Notice the lack of line numbers.)
The "S s = S();" line works at global scope, or when preceded by const or static.
The only way to circumvent this is to add an initializer for each array element:
struct S {
	//int[5] x = 0; // what the compiler tries: doesn't work
	//int[5] x = []; // void[0], not int[5]
	//int[5] x = cast(int[5])[]; // non-constant expression
	//int[5] x = [0]; // int[1], not int[5] as above
	// this works, but is tedious for long arrays
	// (although it can be automated with templates)
	int[5] x = [0,0,0,0,0];
}
Comment 1 Jari-Matti Mäkelä 2007年06月26日 14:26:35 UTC
Fixed in 1.017.


AltStyle によって変換されたページ (->オリジナル) /