This chapter looks at a practical example from science and engineering that can find applications in almost any numerical code. Along the way, you’ll learn some important new concepts and get a taste of metaprogramming at a high level using the MPL. Also, this chapter explains the difference between assignment and initialization in C++ in simple terms, with examples to illustrate each.
Just out of curiosity; Why do all websites that have code examples think it is fun to show the code in <10 px font size?
the browsers do that automatically when the “< pre >” tag is used
does anyone use this stuff to solve real problems? its unreadable even for C++
A big problem with code written by academics is that it quickly becomes extremely cryptic.
A small part of that is c++, a big part of it is some doing stuff fancy just because they can.
Boost is also annoying to the point that it’s a pretty large scale library and using parts of it requires having lots and lots of boost dependencies.
It seems to be powerful but pretty damn hard to get up to speed with it. Needless to say, at work we don’t use it, mostly for the reasons above.
Just wish they could use something else than the <pre> tag, so to code examples could be readable, i had to copy the into Dev-C++ and look at it there.
http://bitsofnews.com
In Firefox you can easily fix the small font in <pre> tags: Go to Tools > Options > Fonts & Colors > Monospace, and increase the size.
I’ve used a lot of STL for scientific problems during the last 4 years. STL is a very helpful tool for coding from general laws (e.g. light diffraction) to specific cases (the light diffraction from a rectangular source traversing different media).
I didn’t know about MPL before reading the article, but I can say that templating data to giving it a physical sense is really helpful. The main “problem” about MPL that it seems that it requires a lot of supplemental code only to specify that your variable “Speed” is m/s. But, in the other hand, once you write the templates for the all physical transformations that you need, and just as any other template, you can reuse them straightforwardlly in any other program or modification of your original program.
The main advantage of MPL is you can discover an operation without physical meaning during compiling time, or at least an operation that wasn’t still considered. That gives to the scientific programmer a very nice help in order to avoid coding errors that make you spent hours before discovering that you mess one operation. A very very simplistic example: you code “Speed = Distance”, instead “Speed = Distance/Time”. With floating types, of course there’s no error. But with MPL, the assignation is not valable. Of course in “real world” situations physical operations are not so simple like “Speed”.
Maybe MPL is not applicable for most of real world applications, but just as the article states, for engineering and scientific problems it can help a lot. And trust me on this, there’s a lot of people in the world spendig in coding/debugging time with this kind of problems. Any help like this is welcome.
Maybe things like physics engines too? Next time you code a game.. think about it.
And it’s great.
It does appear initially to be art for art’s sake, but it does build to a nice conclusion; you see a nice finite state machine laid out with the techniques in the book.
Admittedly, I haven’t done anything with it in Real Life (yet), but there is phenomenal value in Boost, which is truly the CPAN of CPP.
Responding to another remark, the Boosters are not academics; they are real coders doing production things. The regular expressions, boost::python, smart pointers, and so on make it a splendid collection of tools and an excellent resource for learning about C++ (or programming in general)
Nobody doubts the power of boost. The reason it is not widely adopted is that the C++ template mechanisms they require are impenetrable by all but the most dedicated programmers. Not to mention trying to debug this crap, if you accidentally do something that seems reasonable but turns out to be a hidden pitfall. Indeed, it is code like that found in TFB that makes me run away from C++ screaming to any of a number of languages: Java being the current favorite.
A big problem with code written by academics is that it quickly becomes extremely cryptic.
More accurately, that’s a big problem with code written by C++ programmers. The current C++ intelligentsia are infatuated by Lisp, but stuck with a language that can only vaguely emulate it.
The current C++ intelligentsia are infatuated by Lisp, but stuck with a language that can only vaguely emulate it.
Uhm, where did you get that idea? And who (what?) is the “current C++ intelligentsia”, anyway? Like any programming language, C++ is good at some things and not so good at others. And for some of us C++ gets the job done, whereas Lisp/Java/C#/<your language of choice> does not. C++ may not be the most elegant language for your particular area of interest, but C++ works quite well for a whole lot of things.
The reason it is not widely adopted…
Well, I don’t know about you, but I’ve used boost libraries in every single project I’ve worked on in the last 3 years or so, and saved myself countless hours of work in the process.
Nobody doubts the power of boost. The reason it is not widely adopted is that the C++ template mechanisms they require are impenetrable by all but the most dedicated programmers.
I think it depends a lot on what book you learnt from. It seems like 95% or more of C++ books are actually just “C with Classes.” I know the book I used in college was just straight C, except that it used cout instead of printf. It was not until the last section of the book that classes were even mentioned! No wonder some people don’t like C++ – what they are taught is actually pre-standard C++ and horrors like MFC. Those books belong in a museum, not a bookstore.
Templates are actually very easy to use. Once you learn templates then you will start to think all casting and macro hacks are ugly. The C++ Programming Language is a good book to start with for this.
I don’t think you really understand the situation. I’m not talking about the people who use C++ because it works for them. I’m talking about the academics. The people designing C++ 0x, the people writing the forward-looking books (not “Learn C++ in 30 days”.) Stroustrup, Sutter, Alexandrescu, the Boost folks, etc. They’ve brought a lot of cool stuff to C++ (eg: the STL, a lot of the Boost libraries), but also seem to have this fascination with abusing the template mechanism to do Lisp tricks. The results are verbose and error-prone, to say the least.