Hi, in C and C++ you can use #define to substitute a value in place of an identifier while preprocessing. If you initialize a new string and don't change its value after that, will the compiler substitute the string identifier with its value, like #define in C, or will it make a string in memory and refer to that?
Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
March 14 #define-like behavior | ||||
---|---|---|---|---|
| ||||
March 14 Re: #define-like behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeremy | On Tuesday, 14 March 2023 at 05:47:35 UTC, Jeremy wrote: >Hi, in C and C++ you can use #define to substitute a value in place of an identifier while preprocessing. If you initialize a new string and don't change its value after that, will the compiler substitute the string identifier with its value, like #define in C, or will it make a string in memory and refer to that? Manifest constants in D have a similar effect as #defined values, e.g.:
Here, the effect is the same as if you'd written The same is true for string literals:
String literals get special treatment from the compiler in that they are "interned".
The literal "What's up?" should be stored in the binary only once, so both s1 and s2 will point to the same location. Substitute a manifest constant and the effect should be the same:
Just be aware that there's a consequence for array literals:
This allocates two dynamic arrays, not one. Everywhere you use |
March 14 Re: #define-like behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jeremy | On Tuesday, 14 March 2023 at 05:47:35 UTC, Jeremy wrote: >Hi, in C and C++ you can use #define to substitute a value in place of an identifier while preprocessing. If you initialize a new string and don't change its value after that, will the compiler substitute the string identifier with its value, like #define in C, or will it make a string in memory and refer to that? In D, you can get #define-like behavior by declaring the string as a manifest constant, like this:
For more tips on how to translate C preprocessor idioms to D, take a look at "The C Preprocessor vs D" in the "Articles" section of dlang.org. |
March 14 Re: #define-like behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | On Tuesday, 14 March 2023 at 06:20:29 UTC, Mike Parker wrote: >On Tuesday, 14 March 2023 at 05:47:35 UTC, Jeremy wrote: >[...] Manifest constants in D have a similar effect as #defined values, e.g.: [...] Thanks a lot! |
March 15 Re: #define-like behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mike Parker | Just out of curiosity: |
March 15 Re: #define-like behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to bomat | On Wednesday, 15 March 2023 at 16:40:52 UTC, bomat wrote: >Just out of curiosity: It's shorthand for defining an unnamed
D lets you leave out the |
March 15 Re: #define-like behavior | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Wednesday, 15 March 2023 at 19:27:19 UTC, Paul Backus wrote: >It's shorthand for defining an unnamed Ah, I see. Thanks! |