Const spec status.

Simen Kjaeraas simen.kjaras at gmail.com
Sun Apr 27 15:53:50 PDT 2008


Bruno Medeiros <brunodomedeiros+spam at com.gmail> wrote:

> module test;
>
> import std.stdio;
>
>
> struct Struct {
> 	int x;
> }
>
> void main()
> {
>
> 	// helper objects
> 	int num = 10;
> 	invariant(int) inv_num = 1;
> 	invariant(int[]) inv_intArray = [1, 2];
> 	invariant(Object) inv_object = cast(invariant) new Object();
> 	invariant Struct inv_Struct = { 1 };
>
>
> 	/** 6 Const cases **/
>
> 	// Case 1
> 	int a = inv_num;

Works. inv_num is a POD type.

> 	// Case 2
> 	invariant(int) b = num;

Works. See above.

> 	// Case 3
> 	const(int)[] c = inv_intArray;

Works. invariant is implicitly castable to const.

> 	// Case 4
> 	int[] d = inv_intArray;

Does not work.

> 	// Case 5
> 	Object e = inv_object;

Does not work.

> 	// Case 6
> 	Struct f = inv_Struct;

Works. Struct is POD.

> }


Interesting thing I found (because I was unsure of the Struct):


struct foo
{
	int* bar;
}


int baz;

invariant foo f = { &baz }; // this works. I'd believe it is a bug.

foo g = f; // also works. Might be even worse.


-- Simen



More information about the Digitalmars-d mailing list