Top level array constness discarding and Variant

Jonathan M Davis jmdavisProg at gmx.com
Mon Jul 30 13:56:19 PDT 2012


On Monday, July 30, 2012 23:44:56 cybevnm wrote:
> During initializing Variant, D discards top level const of array, which
> leads to little unintuitive behaviour. Consider code:
> 
> import std.stdio;
> import std.variant;
> void main()
> {
> const int[] arr;
> Variant v = Variant( arr );
> writeln( v.peek!( typeof( arr ) )() );
> writeln( v.peek!( const(int)[] )() );
> writeln( v.type() );
> }
> 
> ...and output:
> %dmd main.d && ./main.d
> null
> 7FFF358AE298
> const(int)[]
> 
> As you can see peek works successfully not for original array type, but
> for type without top level const. Is Variant supposed to work in that way ?

Probably not. When arrays are passed to templated functions, they're passed as 
tail-const (so the constness on the array itself - but not its elements - is 
stripped), which in general is _way_ more useful than passing them as fully 
const. However, Variant predates that behavior by quite a while, and it's 
well-passed due for having extensive work done on its implementation (it's API 
should be fine, but it was implemented when D was much younger, and we can do a 
much better job of it now). There's a discussion on that in the main newsgroup 
at the moment actually.

In any case, please create a bug report for this:

http://d.puremagic.com/issues

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list