Request assistance converting C's #ifndef to D

Andrew Edwards via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 12 16:40:09 PDT 2016


On 5/13/16 8:00 AM, H. S. Teoh via Digitalmars-d-learn wrote:
> On Fri, May 13, 2016 at 07:51:17AM +0900, Andrew Edwards via Digitalmars-d-learn wrote:
>> The following preprocessor directives are frequently encountered in C
>> code, providing a default constant value where the user of the code
>> has not specified one:
>>
>> 	#ifndef MIN
>> 	#define MIN     99
>> 	#endif
>>
>> 	#ifndef MAX
>> 	#define MAX     999
>> 	#endif
>>
>> I'm at a loss at how to properly convert it to D. I've tried the
>> following:
>>
>> 	enum MIN = 0;
>> 	static if(MIN <= 0)
>> 	{
>> 		MIN = 99;
>> 	}
> [...]
>
> 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.

> 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