Rainer Deyke wrote:
On 11/10/2010 19:34, bearophile wrote:
Do you seen anything wrong in this code? It compiles with no errors:

enum string[5] data = ["green", "magenta", "blue" "red", "yellow"];
static assert(data[4] == "yellow");
void main() {}


Yet that code asserts.

Wait, what?  That's a static assert.  How can it both assert and compile
with no errors?

As it turns out, the joining of adjacent strings is a critical feature.
 Consider the following:
  f("a" "b");
  f("a" ~ "b");
These are /not/ equivalent.  In the former cases, 'f' receives a string
literal as argument, which means that the string is guaranteed to be
zero terminated.  In the latter case, 'f' receives an expression (which
can be evaluated at compile time) as argument, so the string may not be
zero terminated.
 This is a critical difference if 'f' is a (wrapper
around a) C function.

But in D, unlike C and C++, constant folding is guaranteed to happen for built-in types (and it happens in the semantic pass, not in the backend). So in reality, there's no difference. The docs just need to state that (string literal ~ string literal) is still null terminated -- that's the only thing that is missing.

Reply via email to