Home > General Development > C++: Use Traits Classes for Information About Types C++: Use Traits Classes for Information About Types Eugenia Loli 2005-07-24 General Development 10 Comments One of the strengths of C++ is the inheritability of classes. Learn how to use traits classes for information about types in this sample chapter from ‘Effective C++: 55 Specific Ways to Improve Your Programs and Designs’. About The Author Eugenia Loli Ex-programmer, ex-editor in chief at OSNews.com, now a visual artist/filmmaker. Follow me on Twitter @EugeniaLoli 10 Comments 2005-07-24 9:56 pm I really don’t mean to troll, but come on, the strength of C++ is in its ability to inherit classes? Who wrote this? As far as I know, any object system allows inheritance, maybe not multiple (maybe not mixins with before/after methods like in CLOS), but that’s a strength of using objects, not C++. 2005-07-25 12:14 am This article is about a pattern using generic programming (templates), not inheritance. Generic programming is really neat once you delve past the standard examples (stacks, and the max function). You can manipulate objects polymorphically that are completely unrelated by type. They just have to agree to have common subtypes or method names. This blew people’s minds when they first started experimenting with it 15 years ago, and was largely responsible for the rapid escalation in features that culminated in the C++ standard. BTW Meyers is really late to this party. I’m pretty sure this is his first exposition of the subject, and it’s been around for ten years or more. Paul G 2005-07-25 5:40 am Bastian You can manipulate objects polymorphically that are completely unrelated by type. They just have to agree to have common subtypes or method names. This blew people’s minds when they first started experimenting with it 15 years ago. I believe Objective-C was doing this that far back, too. What makes C++’s way of doing this better than what you can get from message passing like in ObjC, or interfaces like in Java? 2005-07-25 7:35 am I believe Objective-C was doing this that far back, too. What makes C++’s way of doing this better than what you can get from message passing like in ObjC, or interfaces like in Java? The advantage is performance, the compiler knows exactly which type it is dealing with when a template is instanciated and there’s no dynamic dispatch. 2005-07-25 3:20 am pravda Many better books came out on C++ a while back, before Java. So many people don’t know much about the good books for C++. “Effective C++” came out quite a while back and is worth reading once. The book mentioned in the link is the third edition of “Effective C++”. So today, this would be the best version to read. Here is a blurb about the book: “C++ and the C++ community have grown up in the last fifteen years, and the third edition of Effective C++ reflects this. The clear and precise style of the book is evidence of Scott’s deep insight and distinctive ability to impart knowledge.” — Gerhard Kreuzer, Research and Development Engineer, Siemens AG 2005-07-25 10:22 am taschenorakel This article is great, but once again confirms my prejudices against C++. It is a very powerful and efficient language, but somewhere elegance and readability has been lost on the path. Hey, if you develop some program you should throw your brainpower on the problem to solve, not on smart ways to cheat the compiler. 2005-07-26 12:37 am Calroth This article is great, but once again confirms my prejudices against C++. It is a very powerful and efficient language, but somewhere elegance and readability has been lost on the path. Maybe this isn’t because C++ is designed to be an elegant and readable language. Maybe this is because C++ is designed to be an object-oriented version of C. C was never very readable either. It is elegant, but you lose some of that when adding object-oriented concepts. If you want a language something that’s elegant and readable, seriously, go use it. Nobody is forcing you to use C++. You probably have something like Ruby or Eiffel in mind – enjoy them, but don’t criticise C++ for being something it was never meant to be. 2005-07-25 5:15 pm I have a K&R and Stroustrup’s C++ PL 3rd Ed. and think very highly of each but am now looking for good usage and style guides. I have heard high praise for Meyers’ “Effective …” books, but I have never laid hands upon any copies, and comments here seem mixed so far. Anyone have an especially strong (positive or negative) recommendation? Thanks, Jeff 2005-07-25 6:03 pm Latem I cannot say anything bad about this book. I cannot imagine writing C++ without first reading this book, and having it close by for reference and lookup on the details of the items. The book, as its name implies, is a collection 55 specific “items” that you should use as rule of thumb when designing and writing C++ code. The main thing is that most of these items are pretty specific, and are very practical and applicable. Basically, almost all items apply almost all of the time. As the auther says, it may be possible that the item doesn’t apply to your situation, but you should think hard before breaking any of his rules. The 3rd addition is updated with new references to stl, and boost library, and additionlly addresses some threading issues. Overall, I think it’s an awesome book. There is more (and undoubtly better) reviews on amazon. Just search it. On amazon.com it is rated 5 stars based on 102 reviews. 2005-07-25 6:09 pm Yet another reason why I’m giving up C++. I find myself using C, Python, and Java more and more. Good riddance.