Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #19160

Re: template metaprogramming in C?

From "BartC" <bc@freeuk.com>
Newsgroups comp.lang.c
Subject Re: template metaprogramming in C?
Date 2012-03-30 10:52 +0100
Organization A noiseless patient Spider
Message-ID <jl3vsv$5o2$1@dont-email.me> (permalink)
References (3 earlier) <jkukir$cvn$1@speranza.aioe.org> <pan.2012.03.29.15.31.08.620000@nowhere.com> <jl2kq2$isg$1@speranza.aioe.org> <20120329171146.904@kylheku.com> <meta-20120330024837@ram.dialup.fu-berlin.de>

Show all headers | View raw


"Stefan Ram" <ram@zedat.fu-berlin.de> wrote in message 
news:meta-20120330024837@ram.dialup.fu-berlin.de...
> Kaz Kylheku <kaz@kylheku.com> writes:
>>If you want to do anything "meta" in C,
>>you don't want to do it in C.

> #include <stdio.h>
>
> int main( void )
> { char const * const names[] ={ "maxi", "maxd" };
>  char const * const types[] ={ "int", "double" };
>  printf( "#include <stdio.h>\n\n" );
>  for( unsigned i = 0; i < sizeof types / sizeof 0[ types ]; ++i )
>  { char const * type = i[ types ];
>    printf
>    ( "%s %s( %s const a, %s const b ){ return a > b ? a : b; }\n",
>      type, i[ names], type, type ); }
>  printf( "\nint main( void )\n" );
>  printf( "{ printf( \"%%d\\n\", maxi( 2, 3 ));\n" );
>  printf( "  printf( \"%%g\\n\", maxd( 2.0, 3.0 )); }\n" ); }
>
>  It contains the pattern "a > b ? a : b" to find a maximum and then
>  applies this pattern to the types of »int« and »double«.

Is this meta-programming, or just programming? It looks like something that 
would be run as an external utility, rather than a piece of meta-code 
encountered within a normal program, and the output of which is then 
available to the rest of the source code.

(And if run as an external program, then anything slightly more complex 
would be less painful to write with any scripting language.)

And even as a C meta-program, if shows the limitations of using C: you 
really want to loop through all the numeric types, and have access to the 
names of those types, as well as literals in each of those types, without 
having to manually enumerate each.

Try out a real task (which I have a use for myself, and currently use 
external utilities for, generating include files):

You have a number of functions in a module, some of which have textual 
meta-data (in the form of a stylised comment for example); scan those 
functions with a specific kind of meta-data, read the values listed in the 
metadata, and create a table (an initialised array of structs perhaps), 
containing pointers to those functions, the names of the functions, and the 
values for those functions.

And put the table at the location where the meta-program is encountered.

As an added bonus, create a list of prototype declarations suitable for 
adding to a header (not strictly necessary, as you might expect to create 
such prototypes yourself at the same time as you write each function).

(Actually, this is not very easy. It would require a separate pass of it's 
own from the compiler. It might get recursive, if the meta-program adds new 
functions which themselves have meta-data! Or if the meta-program generates 
a new version of itself. However, if this was possible, it would be great: 
forget my example; you can create a meta-program macro which automatically 
generates function prototypes for example.)

-- 
Bartc

 

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-24 11:57 +0000
  Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-24 07:25 -0700
    Re: template metaprogramming in C? Markus Wichmann <nullplan@gmx.net> - 2012-03-26 11:46 +0200
      Re: template metaprogramming in C? Philipp Klaus Krause <pkk@spth.de> - 2012-03-27 21:09 +0200
        Re: template metaprogramming in C? Markus Wichmann <nullplan@gmx.net> - 2012-03-28 09:25 +0200
          Re: template metaprogramming in C? James Kuyper <jameskuyper@verizon.net> - 2012-03-28 06:59 -0400
      Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-28 12:13 -0700
      Re: template metaprogramming in C? James Dow Allen <jdallen2000@yahoo.com> - 2012-04-01 02:47 -0700
  Re: template metaprogramming in C? Jens Gustedt <jens.gustedt@loria.fr> - 2012-03-24 18:01 +0100
  Re: template metaprogramming in C? Nobody <nobody@nowhere.com> - 2012-03-24 22:57 +0000
    Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 18:29 -0700
      Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-25 15:03 +1300
      Re: template metaprogramming in C? nick_keighley_nospam@hotmail.com - 2012-03-25 05:32 -0700
  Re: template metaprogramming in C? BGB <cr88192@hotmail.com> - 2012-03-24 17:02 -0700
  Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-25 01:21 +0100
    Re: template metaprogramming in C? Philipp Klaus Krause <pkk@spth.de> - 2012-03-27 21:17 +0200
      Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-28 11:10 +0200
        Re: template metaprogramming in C? Nils M Holm <news2009@t3x.org> - 2012-03-28 10:05 +0000
          Re: template metaprogramming in C? Willem <willem@toad.stack.nl> - 2012-03-28 13:36 +0000
            Re: template metaprogramming in C? Nils M Holm <news2009@t3x.org> - 2012-03-28 14:40 +0000
        Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-28 18:32 +0000
          Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-28 22:27 +0200
            Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-28 21:02 +0000
        Re: template metaprogramming in C? Nobody <nobody@nowhere.com> - 2012-03-29 16:30 +0100
          Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-03-29 23:39 +0200
            Re: template metaprogramming in C? "BartC" <bc@freeuk.com> - 2012-03-29 23:36 +0100
              Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-30 00:55 +0000
            Re: template metaprogramming in C? Kaz Kylheku <kaz@kylheku.com> - 2012-03-30 00:32 +0000
              Re: template metaprogramming in C? "BartC" <bc@freeuk.com> - 2012-03-30 10:52 +0100
                Re: template metaprogramming in C? jacob navia <jacob@spamsink.net> - 2012-04-01 09:36 +0200
            Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-30 16:32 +1300
              Re: template metaprogramming in C? Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-03-31 01:52 -0700
    Re: template metaprogramming in C? ImpalerCore <jadill33@gmail.com> - 2012-03-28 08:48 -0700
      Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 12:18 +0200
        Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:08 +0200
          Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:36 +0200
            Re: template metaprogramming in C? pete <pfiland@mindspring.com> - 2012-04-06 18:43 -0400
              Re: template metaprogramming in C? pete <pfiland@mindspring.com> - 2012-04-06 19:20 -0400
      Re: template metaprogramming in C? "io_x" <a@b.c.invalid> - 2012-04-05 13:08 +0200
  Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 19:01 -0700
    Re: template metaprogramming in C? William Ahern <william@wilbur.25thandClement.com> - 2012-03-24 19:10 -0700
  Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-25 15:25 +1300
    Re: template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-25 15:13 +0100
      Re: template metaprogramming in C? Ian Collins <ian-news@hotmail.com> - 2012-03-26 07:10 +1300
        Re: template metaprogramming in C? Rui Maciel <rui.maciel@gmail.com> - 2012-03-25 20:31 +0100
          Re: template metaprogramming in C? nick_keighley_nospam@hotmail.com - 2012-03-26 00:54 -0700

csiph-web