Request assistance converting C's #ifndef to D

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 12 23:48:36 PDT 2016


On 5/13/16 3:23 PM, tsbockman wrote:
> On Friday, 13 May 2016 at 06:05:14 UTC, Andrew Edwards wrote:
>> Additionally, what's the best way to handle nested #ifdef's? Those
>> that appear inside structs, functions and the like... I know that
>> global #ifdef's are turned to version blocks but versions blocks
>> cannot be used inside classes, stucts, functions, etc.
>
> `static if` and `version()` can be nested, and both work just fine
> inside classes, structs, functions, etc.:
>
> module app;
>
> version = withPrint;
>
> struct A {
>    version(withPrint) {
>      class B {
>        static if(size_t.sizeof == 4) {
>          static void print() {
>            import std.stdio : writeln;
>            version(unittest) {
>              writeln("Hello, 32-bit world of unit tests!");
>            } else {
>              writeln("Hello, 32-bit world!");
>            }
>          }
>        } else {
>          static void print() {
>            import std.stdio : writeln;
>            version(unittest) {
>              writeln("Hello, presumably 64-bit world of unit tests!");
>            } else {
>              writeln("Hello, presumably 64-bit world!");
>            }
>          }
>        }
>      }
>    }
> }
>
> void main() {
>    A.B.print();
> }

Not sure what I was doing wrong earlier. Works perfectly fine now. Glad 
I asked because I usually just get frustrated and put it aside and 
usually never return to it. Thanks for the assist.

> (Try it on DPaste: https://dpaste.dzfl.pl/0fafe316f739)



More information about the Digitalmars-d-learn mailing list