translate a macro to D
teo
teo.ubuntu at yahoo.com
Thu Jul 7 08:38:05 PDT 2011
On Wed, 06 Jul 2011 16:21:31 +0200, Trass3r wrote:
> Am 06.07.2011, 16:15 Uhr, schrieb teo <teo.ubuntu at yahoo.com>:
>
>> What is the best way to translate following to D?
>>
>> #define MAKELONG(a, b) \
>> ((LONG) (((WORD) (a)) | ((DWORD) ((WORD) (b))) << 16))
>>
>> The point is I would like to be able to use that at compile-time. The
>> macro is supposed to define some constants.
>
> Just turn it into a function.
> If you assign it to an enum or use it as an initializer for global
> immutables it should be evaluated at compile-time.
Thank you. That did the trick.
However I am facing another problem:
There is a macro, which obtains the size of a type
#define TYPESIZE(t) (sizeof(t))
Example:
printf("%ld\n", TYPESIZE(short));
printf("%ld\n", TYPESIZE(struct A));
The TYPESIZE macro is used within another macro which defines constants.
#define M(a,b,size) \
((a) << SHIFT_A) | \
((b) << SHIFT_B) | \
((size) << SHIFT_SIZE))
#define MM(a,b,type) M((a),(b),(TYPESIZE(type)))
Example:
#define C1 MM(1, 2, struct A)
#define C2 MM(3, 4, struct B)
How can I translate that to D?
More information about the Digitalmars-d-learn
mailing list