Dude! Where's my string constant?

Chris Nicholson-Sauls ibisbasenji at gmail.com
Tue Jan 23 13:31:59 PST 2007


kris wrote:
> Does D have a compile-time string constants? One that doesn't wind up in 
> the obj file when it is unused?
> 
> const char[] Foo = "foo";
> 
> Apparently resides in the object file, used or not. Just like const int 
> does. For true compile-time integer constants, we're supposed to use 
> enum -- but what of strings? Enum currently doesn't like char[] :)
> 
> Suggestions for faking it include:
> 
> * char[] Foo() { return "foo"; }
> * template Foo { const Foo="foo"; }
> 
> The former *should* get removed by the linker as unused, while the 
> latter apparently gets eliminated by way of lack-of-expansion.
> 
> However, going to these lengths is surely counter-intuitive?
> 

Personally, I've always thought it would be useful if enums could take any type as base. 
So then you would just:

enum : char[] { Foo = "foo"c }

I vaguely recall posting about this a few years ago and getting a generally negative response.

-- Chris Nicholson-Sauls



More information about the Digitalmars-d mailing list