string literals

Kenji Hara k.hara.pg at gmail.com
Fri May 31 11:45:21 PDT 2013


On Friday, 31 May 2013 at 14:20:45 UTC, Jack Applegame wrote:
> What's the reason that the string literal is a dynamic array, 
> not a static?
> So sometimes it is not possible to get string length compile 
> time:
>
> void foo(T: E[N], E, size_t N)(auto ref T data) {
>     pragma(msg, "static");
>     pragma(msg, data.length);
> }
> void foo(T: E[], E)(auto ref T data) {
>     pragma(msg, "dynamic");
>     pragma(msg, data.length);
>     // Error: variable data
>     // cannot be read at compile time
> }
> ...
> foo("test");

With 2.063, this code works.

void foo(E, size_t N)(ref E[N] data) {
     pragma(msg, "static, E = ", E, ", N = ", N);
}
void foo(E)(E[] data) {
     pragma(msg, "dynamic, E = ", E);
}
void main() {
     foo("test");        // static
     auto s = "test";
     foo(s);             // dynamic
}

Kenji Hara


More information about the Digitalmars-d-learn mailing list