The CAPI Manifesto

so so at so.so
Fri Oct 21 03:17:32 PDT 2011


Indeed, macros is a language in itself.
Then again it all boils down to type manipulation and function calls.
Not sure if it is doable but a special operator like "__cmacro" might be  
an answer.

#define FUN(a, b) ....
#define DATA ....

could be accessed like:

__cmacro(FUN, a, b);
__cmacro(DATA);

I am pushing this because the outcome well worths all these ugly hacks.

On Fri, 21 Oct 2011 11:32:32 +0300, Gor Gyolchanyan  
<gor.f.gyolchanyan at gmail.com> wrote:

> That's ALL you can do in C. fill structs and call functions
> (fundamental type manipulation doesn't count).
> My personal research shows the following use cases of C macros (sorted
> by popularity in descending order):
> 1. enum
> 2. alias (most notably, conditionally compiled ones)
> 3. CTFE function
> 4. mixin template
> 5. syntactic alias
> 6. syntactic mixin template
>
> only the last 2 out of 6 cannot be translated to D.
> An example of a syntactic alias is this very common piece of C code:
> #ifdef __VERY_VERY_OLD_C_COMPILER__
>     #define CONST
> #else
>     #define CONST const
> #endif
>
> An example of a syntactic mixin template is this piece of code, which
> i never actually saw anywhere (possible only in C99 and C++):
> #define N_TIMES(n) for(int i = 0; i != n; ++i)
>
> The last use case is very rare. The only legitimate example i ever saw
> is in libjpeg, where a macro is used to define function pointers of
> API functions.
> The use case before that is mostly used for portability reasons, which
> is not necessary in D.
> Some non-standard extension encapsulating macros are almost always
> used in C libraries, which can be removed altogether.
>
> The translation can go on regarding the above use cases and the last
> two cases can be evaluated in-line, commented out and warned about for
> manual translation.


More information about the Digitalmars-d mailing list