static array with inferred size

Dgame r.schuett.1987 at gmail.com
Wed Sep 20 15:48:19 UTC 2017


On Wednesday, 20 September 2017 at 14:15:30 UTC, Steven 
Schveighoffer wrote:
> On 9/20/17 3:12 AM, Dgame wrote:
>
>> 
>> http://p0nce.github.io/d-idioms/#@nogc-Array-Literals:-Breaking-the-Limits
>
> This is plain stack corruption, you should fix that. The s 
> template seems useful, but still I can't get my use case to 
> work with it:
>
> T[n] s(T, size_t n)(auto ref T[n] array) pure nothrow @nogc 
> @safe
> {
>     return array;
> }
>
> void main()
> {
>     char[5] x1 = "hello"; // works.
>     auto x2 = s("hello"); // oops, type is immutable(char[5]);
>     auto x3 = s!char("hello"); // template s cannot deduce 
> function from argument types !(char)(string)
> }
>
> -Steve

Works:
----
char[5] b = "hallo".s;
----

Otherwise you could simply use Unqual:
----
Unqual!T[n] s(T, size_t n)(T[n] arr)
{
	return arr;
}

auto a = "hallo".s;
writeln(typeof(a).stringof); // char[5]
----




More information about the Digitalmars-d mailing list