Sign in to follow this  
kevinlynx

how to study the generic programming ?

Recommended Posts

kevinlynx    122
i know how to learn a language : just write enough codes and read more codes. But about the generic programming in c++ seems difficult to learn because there are so many language details to pay attention . And i donot know how to practice the gp what i've learned . i think i should get some thoughts about the gp but not only learn these ****ing details . You see, it's dull and easy to forget . :P. So , can you share your experience about the generic programming with me ? I just want to be an expert of the c++ but not a common user :D. Thanks in advance .

Share this post


Link to post
Share on other sites
Antheus    2409
You can't become an expert by writing code. Proficient, well versed perhaps, but not an expert, not without studying the foundation and the concepts.

Generic programming, like object oriented programming, is a concept, not code. As such, it contains very little details. There are obviously implementation details, specifications and standards, such as those covering the behaviour iterators and containers, but those are no different from other comparable classes in other languages.

But the important thing here is that most of these details make sure that users of the classes can use them intuitively.

Generic programming as offered by STL for example, also relies heavily on algorithms and their application. It allows you to completely forget the code and and the details. No more memory management, alignements, ownership issues, and all those pesky annoying details. Instead, the focus shifts to working with conceptual classes. If your typical idea of plain C++ is using for loop to iterate int array, the GP equivalent is traversing a collection with a functor. And that makes a whole world of difference at a higher level.

But keep in mind, generic programming is not API. There is not a set of predefined functions you could memorize and consider it done.

Share this post


Link to post
Share on other sites
small_duck    294
Here's the "bible" on generic programming: Modern C++ Design, by Andrei Alexandrescu. It is really an eyes-opening book on what you can do with templates.

After that, it is a matter of actually forcing yourself, when you program, to look for the cases where some more generic programming would be useful. Look for bits of code that look similar, and try to factor them. When writing a data structure, think how you could abstract the fundamental types.

Write a lot of code, and use a lot of code: by using STL and Boost as much as possible, you will get a feeling on when generic programming is useful, and how it should be written.

Share this post


Link to post
Share on other sites
kevinlynx    122
thanks for your quick reply . Is <Moden C++ Design> difficult to understand ? And how about <C++ template> ? I have read these chapters before chapter8 . But after chapter 8 it seems these things are all language details ! I donot think learning these details is good . Because if i donot use these things in practice i 'll forget them you know .
I really think i should get some generic programming 's thoughts .--it's about the generic programming feelings .

Share this post


Link to post
Share on other sites
Wyrframe    2426
"Forgetting the details because you don't use them in practice..." The solution to this is, of course, to use them in practice.

If you learn the "language details" of numerous languages, you'll eventually start pulling together a mental model at a higher level than the languages themselves. Generic programming is about being able to take an idea and write it in any language, any toolset, any environment.

Additionally, learning how to design programs at the source-code level is far more important than learning how to program at the source-code level. Knowing the tools at your disposal is a big part of that; if you don't know how to use C++ Templates or Java Generics, how will you make use of them?

Share this post


Link to post
Share on other sites
CTar    1134
Quote:
Original post by kevinlynx
Is <Moden C++ Design> difficult to understand ?

Not if you know some basic C++ (including templates, inheritance and classes).

Quote:
And how about <C++ template> ? I have read these chapters before chapter8 . But after chapter 8 it seems these things are all language details ! I donot think learning these details is good . Because if i donot use these things in practice i 'll forget them you know .

Templates is a very important part of C++ and one of the main reasons C++ is better than C (not that templates would be terribly useful in C without proper UDTs). AFAIK templates is the only way to get type-safe parametric polymorphism in C++, so it's very important (and some languages implement a similar feature called generics).

Concepts like paramtric polymorphism, metaprogramming, specialization, etc. are not just language details.

Quote:
I really think i should get some generic programming 's thoughts .--it's about the generic programming feelings .

If you intend to do generic programming in modern C++ then you'll have to learn templates.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this