Vladimir Prus


Saturday, November 06, 2004

Macros are evil

C++ macros are evil and should never be used. Right? Ok, then rewrite the following Boost.Graph code without macros and explain how the resulting mess is better.
    BGL_FORALL_VERTICES(v, g, Graph) {
        BGL_FORALL_VERTICES(u, g, Graph) {
            // For 'v', compute edge_label -> edge_descriptor map
            map<int, Graph::edge_descriptor> label2edge;

            BGL_FORALL_OUTEDGES(v, edge, g, Graph) {
                label2edge[get(edge_weight, g)[edge]] = edge;

            // For 'u' edges, find the 'v' edge with the same label,
            // and find the new pair of states.
            BGL_FORALL_OUTEDGES(u, edge2, g, Graph) {
                Graph::edge_descriptor edge = label2edge[
                    get(edge_weight, g)[edge2]];

                // .......


Of course, it does not change the fact that code which uses Boost.Preprocessor tend to be less readable than Perl. And it does not change the fact that we need in-language foreach.

1 comment:

Anonymous said...

ha ha, you code in C++