Live data from Hacker News

Simple C++11 metaprogramming

pdimov.com

1–10 of 11 posts

Re: Simple C++11 metaprogramming

#2
While I know nothing about metaprogramming in C++, the book Metaprogramming Ruby is one of the most well done and thought provoking programming book I have ever read, one I always recommend. Really, even if you hate Ruby, it's worth your while to see how metaprogramming can be used to problem solve, which is ultimately what all this is about.

The most popular Ruby framework, Ruby on Rails, has quite the learning curve because it's based heavily on conventions enabled by metaprogramming. inb4 lengthy discussion of "convention vs configuration" and "magic."

Re: Simple C++11 metaprogramming

#3
This type of C++ code makes me sad. I can follow along and figure out what it's doing, but I really have to slow down and think about it. C++ template metaprogramming has the ugliest syntax of a "real" programming language that I've ever seen. Brainfuck might win for hardest to follow code, but god damn it, C++ is really trying.

IMO, Lisp did metaprogramming the right way, 40 years ago. It's built into the language and doesn't look like a hacked on cluster f*. Anybody familiar with regular Lisp can follow along with Lisp metaprogramming, and it's far more powerful than what's available in C++. And nowadays, the performance is even competitive.

Re: Simple C++11 metaprogramming

#4
post #3

This type of C++ code makes me sad. I can follow along and figure out what it's doing, but I really have to slow down and think about it. C++ template metaprogramming has the ugliest syntax of a "real" programming language that I've ever seen. Brainfuck might win for hardest to follow code, but god damn it, C++ is really trying. IMO, Lisp did metaprogramming the right way, 40 years ago. It's built into the language a…

How did Lisp solve (40 years ago) the problem of generating a compile time errors when types of the arguments into a function call did not match with what the developer of the function anticipated?

Re: Simple C++11 metaprogramming

#6
post #4
post #3

This type of C++ code makes me sad. I can follow along and figure out what it's doing, but I really have to slow down and think about it. C++ template metaprogramming has the ugliest syntax of a "real" programming language that I've ever seen. Brainfuck might win for hardest to follow code, but god damn it, C++ is really trying. IMO, Lisp did metaprogramming the right way, 40 years ago. It's built into the language a…

How did Lisp solve (40 years ago) the problem of generating a compile time errors when types of the arguments into a function call did not match with what the developer of the function anticipated?

When you look at error messages of C++ inside of metaprogramming code you understand that the error reporting in case of argument mismatch is still not solved .... :)

Re: Simple C++11 metaprogramming

#7
post #3

This type of C++ code makes me sad. I can follow along and figure out what it's doing, but I really have to slow down and think about it. C++ template metaprogramming has the ugliest syntax of a "real" programming language that I've ever seen. Brainfuck might win for hardest to follow code, but god damn it, C++ is really trying. IMO, Lisp did metaprogramming the right way, 40 years ago. It's built into the language a…

This type of C++ code also makes me sad. I actually can't follow along. I can't glance at the code and parse what it is doing because the template keyword has many flavours, as does class, as does typename, as does using, as does ..., and so on. and > seem to be the template equivalent of ( and ). I can't tell the intent of a piece of template code quickly.

I agree with your Lisp sentiment. It appears that the C++ folks have spent the last 15 (20?) years re-implementing Lisp macros badly. Every time they work out the kinks in the syntax, remove corner cases, and make it more expressive: it becomes more Lisp-like.

When you compare with the approach Ruby took (granted, Ruby is a dynamic language) you get the feeling that the C++ folks went wrong somewhere. Far be it from me who has never implemented a compiler to criticise them but that's the honest feeling I get. Ruby made everything an object with an associated class. Or what amounts to the same thing, it made every type of thing get-at-able as an object with an associated class. And here is the kicker, including Object and Class itself. Why could the C++ folks not have made type an object with an associated class with compile and runtime methods?

    class Foo {
        ...
    }
is just sugar for

    type Foo = new class();
    Foo.class_eval {
        ...
    }

Re: Simple C++11 metaprogramming

#9
post #4
post #3

This type of C++ code makes me sad. I can follow along and figure out what it's doing, but I really have to slow down and think about it. C++ template metaprogramming has the ugliest syntax of a "real" programming language that I've ever seen. Brainfuck might win for hardest to follow code, but god damn it, C++ is really trying. IMO, Lisp did metaprogramming the right way, 40 years ago. It's built into the language a…

How did Lisp solve (40 years ago) the problem of generating a compile time errors when types of the arguments into a function call did not match with what the developer of the function anticipated?

By allowing developers to implement whatever level of error message reporting they fancy.

Re: Simple C++11 metaprogramming

#10
Cool post. I have always disliked C++ MP because I couldn't see the forest for the trees. Judging by the first few examples (using, mp_rename) C++11 cleans it up and makes it understandable.
Post reply on HN