This chapter provides you with the opportunity to flex your C++ muscles by critiquing a piece of code. Can you find a better way to optimize this code for idiomatic usage? Find out in this challenge from Herb Sutter.
This chapter provides you with the opportunity to flex your C++ muscles by critiquing a piece of code. Can you find a better way to optimize this code for idiomatic usage? Find out in this challenge from Herb Sutter.
This class doesn’t stand up on it’s own. It is useful when used in concert with the STL algorithms and functional bits. For example:
//
// Some class which performs some kind of decoding.
// It needs to know about previous numbers to do anything
// with the next number.
//
class decoder {
public:
void crunchNumber( unsigned int number );
};
…
std::vector<unsigned int> data;
decoder myDecoder;
//
// Populate the data vector here.
//
…
//
// Use the STL for_each algorithm to iterate through all
// of the numbers in the data vector passing them to
// myDecoder.
//
// Note that make_callback is used to create an unary
// functor which invokes the crunchNumbers method of
// myDecoder.
//
std::for_each( data.begin(), data.end(),
make_callback( myDecoder, &myDecoder::crunchNumber ) );
//
// Now do something with the decoded result..
//
So make_callback could be useful in such contexts. Of course, you could write the exact same thing in C (with a for-loop and some kind of opaque context structure for the decoder, but that’s not the point).
–ralpht
The given template is a very small advance over a C struct containing an object pointer and a function pointer. The method signature is hardcoded to take no args and return void, which is likely to be inconvenient at times. And you still pay for the cost of dereferencing the function pointer, even if the method is short enough to be written in-line.
But I don’t have a revised template.
I read the article with great interest, because I have a similar task now:
“if I could have that kind of a list, why couldn’t I have a list of arbitrary kinds of callbacks of various types, so that I can remember them all and go execute them all when I want to?”
Now I use GCC for 16 bit system with no support for sdtc++. So I am not able to use “virtual”. Is there any way I can implement this type of list in C++. I can do it in C but that’s not what I want.
Thank You.
What system do you use ?
Cypress CY7C63700. USB stand alone Host.
But there are other types of 16-bit MCUs with ported GCC and very limited support for C++.
Thank You.
As soon as I read the authors name “Herb Sutter” I knew that the article would be very interesting. He has also written some books called “Exceptional C++”, and I really love them. They may not be very useful for the beginners, but they contain a lot of subtle tips for the advanced C++ developer and even most C++ gurus will find some information they didn’t know before.
The books follow the same style as the article, first a small snippet of code is presented to the reader together with some questions and then the solution is discussed. I like this approach very much, because this way you can always test your own knowledge and try to find all flaws yourself – which can be very tricky!
Kaya
I have often found that there is a third answer to these types of quizzes – if you ever find yourself this deep into C++ minutae, just back out of it and figure out some vastly simplified way of just solving your problem.
So many coders avail themselves of language facilities just to use them, even though Bjarne says explicity in Ch 1 of his C++ guide to beware of this practice. Its an unfortunate newbie trait that kills code over time. What is most unfortunate is that C++ presents to the coder one of the largest and most confusing set of esoteric language features through which the coder might hang themselves.
The equivalent C solution may not be as elegant in the mind of design nerds, but it will most likely be much more durable over the long term.
In all honesty, I read this a couple of hours ago and was wondering whether to post a virtually similar opinion like yours. Yours is excellent and I can fully relate to it after around 14 years of using C++ and several years of writing commercial software in C++. Simplicity is ofter superior to complexity. Writing as much of your program as you can in a common language subset available in most imperative languages, it is possible to do amazing things and reap benefits with code portability and reuse accross such languages. Of course, that doesn’t mean that one should not learn how to do complex stuff in the most syntactically convoluted but still meaningful way possible. Learning that can make you a better programmer. However, knowing that is one thing, knowing when to use it is another. But one thing is sure: in 9.9 out of 10 cases the simpler solution is better, albeit not syntactically elegant or C++ feature-wise.
You said “It’s an unfortunate newbie trait that kills code over time” and that is so true…
Herb Sutter is one of those “it’s not real c++ if it doesn’t use at least 10 templates and a policy class per file” people that have taken over the standards committee of late. C++ is fundamentally a way to solve problems, not to showcase the latest fashion in design patterns.
I use templates fairly regularly: it takes a fair amount of skill to do that in ways that make life better instead of worse. Templates are the easiest way I know to make code that is unreadable, undebuggable, unportable, and unmaintainable (and as a bonus, often takes longer to write and get working) but does save a few lines. I’ve often thought that something like PHP (a super preprocessor, sort of like a lisp macro) would suit me better for a lot of the “generic” programming I do.
While I’ve enjoyed playing with template metaprogramming, for practical use it’s the greatest example of the Emperor’s new clothes I’ve seen since I started programming in the 70’s. Not to be cynical, but has anyone else ever thought that the Template Meister’s, like the UML worshippers, are really just guys that foresaw outsourcing early and set out to invent new reasons that they were indispensable?
I love your comment regarding templates, and have to whole heartly agree. What worries me are things like Generics in Java and .NET. Ok, some will argue Generics are better than in C++, but I then think of your comment…
Undebuggable, unreadable, etc…
My question is why are templates / Generics when used in more complex scenarios so hard? Is it our brain that ain’t wired that way? I am thinking about this because when you consider something like Python and its dynamic facilities it is not as difficult….
My question is why are templates / Generics when used in more complex scenarios so hard? Is it our brain that ain’t wired that way? I am thinking about this because when you consider something like Python and its dynamic facilities it is not as difficult….
I think the main problem is that C++ templates were hacked in late and evolved to their present state, instead of being designed in from the beginning w/ C++ as an example of what not to do. They take a ridiculous amount of typing (in an otherwise terse language) and tweaking and memorizing incantations; C++ also encourages self-styled gurus who like to see just how aesthetically convoluted they can get.
I am not in the least impressed by something done in one line that should be done in 10, with comments. I’m not sure I even WANT to be l33t enough to write that one line faster than the 10 simple ones; most modern compilers will generate the same code in either case.
I often use the preprocessor when a template is the “right” way, because I know it will be done faster and if it doesn’t work I can see why instantly (just hit Preprocess.)