Request assistance converting C's #ifndef to D

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


On 5/13/16 8:40 AM, Andrew Edwards wrote:
>> That seems wrong. You can't assign to an enum. Besides, doesn't your
>> declaration of MIN shadow whatever other definitions may be currently in
>> effect?
>
> Okay, got it. It seams I just hadn't hit that bug yet because of other
> unresolved issues.
>
>> Perhaps what you meant is something like this?
>>
>>     static if (!is(typeof(MIN) : int))
>>         enum MIN = 99;
>
> This seems to do the trick.

But not exactly the way it's expected to. In the snippets below, C 
outputs 10 while D outputs 100;

min.c
=========================
	#define MIN 10 // [1]

	#include "mild.h"

	int main()
	{
     		print();
     		return 0;
	}

min.h
=========================
	#include <stdio.h>

	#ifndef MIN
	#define MIN 100
	#endif

	void print()
	{
	    printf("%d\n", MIN);
	}

minA.d
=========================
	enum MIN = 10; // [1]

	import minB;

	void main()
	{
	    print();
	}

minB.d
=========================
	static if (!is(typeof(MIN) : int))
		enum MIN = 100;

	void print()
	{
     		import std.stdio: writeln;
     		writeln(MIN);
	}

Is there a way to reproduce the same behavior? Are there reason's for 
not allowing this functionality or am I just misunderstanding and going 
about things the wrong way?

[1] same result whether placed before or after the #include/import 
statement.

>
>> though I'm not sure if such a thing will actually work, since
>> order-dependent declarations in D are a kind of dangerous territory to
>> tread on.
>
> So what is the current best practice when encountering such statements
> during porting?
>
>>
>> T
>>
>



More information about the Digitalmars-d-learn mailing list