Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trick : filling array values using macros (code generation)

Tags:

c++

arrays

c

macros

Are C++ Templates just Macros in disguise?

I was reading the above topic, and suddenly this idea came to my mind: why not try writing some tricky macros which can be used in our real code, (not just only as puzzles which are useless in real life)?

So the first thing came to mind is : filling array values with macros:

int f(int &i) { return ++i; }

#define e100     r5(m20)
#define m20      m5,m5,m5,m5
#define m5       r5(e1)
#define e1       f(i)  //avoiding ++i right here, to avoid UB!
#define r5(e)    e,e,e,e,e

int main() {
        int i=0;           //this is used in the macro e1
        int a[] = {e100};  //filling array values with macros!
        int n  = sizeof(a)/sizeof(int);
        cout << "count = " << n << endl;
        for(int i = 0 ; i < n ; i++ ) 
            cout << a[i] << endl;
        return 0;
}

Output:

count = 100
1
2
3
4
.
.
.
100

Online demo : http://www.ideone.com/nUYrq

Can we further improve this solution in terms of compactness or genericity (possibly both)? Can we get rid of the variable i which we need in the macro? Or any other improvement?

I would also like to know if that is valid code both in C++ and C (of course ignoring printing part)?

EDIT:

I realized that the order of calls to f() seems still unspecified. I'm not sure though, as I think comma in array initialization is not probably same as comma operator (in general). But if it is, can we avoid it and what part of the Standard says its unspecified?

like image 379
Nawaz Avatar asked May 21 '11 06:05

Nawaz


2 Answers

If you wish to delve into Preprocessor programming, I can only recommend the Boost.Preprocessor library as a building block, you'll avoid having to rewrite things from scratch.

For example, in order to create your table, I would have used (ideone):

#include <iostream>

#include <boost/preprocessor/repetition/enum.hpp>

#define ORDER(z, n, text) n

int main() {
  int const a[] = { BOOST_PP_ENUM(100, ORDER, ~) };
  std::size_t const n = sizeof(a)/sizeof(int);

  std::cout << "count = " << n << "\n";

  for(std::size_t i = 0 ; i != n ; ++i ) 
    std::cout << a[i] << "\n";

  return 0;
}

And leave all the cruft to Boost :)

Note: this enumerates from 0 to 99, not 1 to 100, there are other operations available to perform arithmetic ;)

EDIT: How does this work ?

First, I can only recommend the doc entry for BOOST_PP_ENUM

BOOST_PP_ENUM is a macro which takes 3 arguments: (n, MACRO, data)

  • n: an integer
  • MACRO: a macro accepting 3 arguments: (z, i, data)
  • data: some data, of your convenience, to be passed to macro

It will then be replaced by n successive invocations of MACRO separated by commas:

MACRO(z, 0, data), MACRO(z, 1, data), ... , MACRO(z, n-1, data)

It is up to you to do whatever you wish with your MACRO.

I am afraid I have never used the z argument, it is used internally, and you could in theory use it to speed up the process.

like image 65
Matthieu M. Avatar answered Sep 18 '22 11:09

Matthieu M.


P99 has a macro that does exactly what you want

#include "p99_map.h"

int Ara[] = { P99_POSS(100) };

It has the advantage that it is entirely compile time, no dynamic initialization with functions etc at all.

For you, it probably has the disadvantage that it uses C99 features, in particular macros with variable length arguments.

like image 40
Jens Gustedt Avatar answered Sep 20 '22 11:09

Jens Gustedt