Request assistance converting C's #ifndef to D

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 12 16:00:33 PDT 2016


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?

Perhaps what you meant is something like this?

	static if (!is(typeof(MIN) : int))
		enum MIN = 99;

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.


T

-- 
What's an anagram of "BANACH-TARSKI"?
BANACH-TARSKI BANACH-TARSKI.


More information about the Digitalmars-d-learn mailing list