Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 01, 2014 Strings concatenated at compile time? | ||||
---|---|---|---|---|
| ||||
In the following example from the documentation, are strings concatenated at compile time? template foo(string s) { string bar() { return s ~ " betty"; } } void main() { writefln("%s", foo!("hello").bar()); // prints: hello betty } |
May 01, 2014 Re: Strings concatenated at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Unwise | On Thursday, 1 May 2014 at 10:42:36 UTC, Unwise wrote:
> In the following example from the documentation, are strings concatenated at compile time?
>
> template foo(string s) {
> string bar() { return s ~ " betty"; }
> }
>
> void main() {
> writefln("%s", foo!("hello").bar()); // prints: hello betty
> }
I guess it's not guaranteed, but constant folding should take care of it, yes.
|
May 01, 2014 Re: Strings concatenated at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Thu, 01 May 2014 11:12:41 +0000 anonymous via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > On Thursday, 1 May 2014 at 10:42:36 UTC, Unwise wrote: > > In the following example from the documentation, are strings concatenated at compile time? > > > > template foo(string s) { > > string bar() { return s ~ " betty"; } > > } > > > > void main() { > > writefln("%s", foo!("hello").bar()); // prints: hello betty > > } > > I guess it's not guaranteed, but constant folding should take care of it, yes. If you want it to be guaranteed, you'd do something like template foo(string s) { enum foo = s ~ " betty"; } void main() { writeln(foo!"hello"); } I would hope that the optimizer would have optimized out the concatenation in your example though. - Jonathan M Davis |
May 01, 2014 Re: Strings concatenated at compile time? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | Jonathan M Davis:
> If you want it to be guaranteed, you'd do something like
>
> template foo(string s)
> {
> enum foo = s ~ " betty";
> }
A more general solution is to wrap the concatenation with a call to:
alias ctEval(alias expr) = expr;
Use:
string bar() { return ctEval!(s ~ " betty"); }
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation