Reposted an article, like STL's friends don't miss: Overdoing Templates (Excessive Template)

zhaozj2021-02-08  457

This article is written by Steve Donovan. He is the author of "C By Examples". He developed a C interpreter called Underc, which can explain C as a scripting language. Compared with Bjarne Stroustrup, Andy Koenig, Scott Meyers, in the C community, his name is much smaller. However, his article opens against the master's opinions, and I think it is very reasonable. In order to show examples of STL capabilities, Masters should not be used as an example of daily programming. And over-emphasizing the code skills, is making C step by step away from ordinary programmers. When all the "rookie" abandon a language, even if the skills of "prawn" are high, they can only entertain themselves. I think this article is a shout, although it is not loud enough, but it is a rare shout -

Let us have no powerful C simple things!

Overdoing C TemplatesEvery decade or so, a new fashion in programming comes along and announces itself the successor to previous discredited paradigms. Again we believe that from now on, software will be more reliable, cheaper to build, and more fun to make. (No . one believes it will be smaller or faster) in the 70s, there was structured programming; in the 80s, object-oriented programming;. and from the mid-90s onward, generic programming This last one gets its name from powerful techniques for code Reuse Using Templates (Generic Classes and functions).

Parameterized classes and functions (templates) are very useful beasts. A function sqr () can be written that will square anything that defines the multiplication operator-complex numbers, matrices, whatever. The standard container classes such as list <> are templates-one Doesn't Have To Rewrite Them. This Was A Genuine Pain PLAIN OLD C , And i Think The ISO Standard Is A Great Improvement. However, Some Things.

For instance, the standard library strings and iostreams are templates parameterized by a "character traits" type. This means that the same basic_string <> class definition can generate ASCII strings, Unicode strings, and Martian Trilobyte strings. (In principle. Many implementations have still only implemented it for ASCII characters, which seems particularly silly.) The standard mandates that these very common classes, which are used in nearly every C application, must be implemented as templates.But there is a price to pay in performance and debuggability. Some experimentation (using Microsoft Visual C 6.0) illustrates the problem. This compiler supports the new-style iostreams library, as well as the "classic" iostreams, so we can compare the implementations. The first test program is of course "Hello, World , "Which Compiles More Than Twice As Fast Using Classic Iostreams. A More Serious Program Involved 200 Lines, Each Writing Out 10 Variables To Count. What is Most Striking IS the compilation speed: It took nearly 10 sec using the standard library versus 1.5 sec for the classic implementation Ten seconds is a lot of time; one can cram a whole highly irritating commercial into that kind of break Executable size was 115K for standard and.. 70K for classic. Your mileage may vary, but the overall picture is of slower builds and larger executables when using the new iostreams library. and this is not a peculiarity of the Microsoft compiler because GCC behaves in the same way.

It's true executable size is not as important as it used to be, but these days the fastest growing class of programmable devices consists of information appliances (in which memory will remain at a premium for the next few years) that: hand-helds, cell phones, smart refrigerators, Bluetooth-enabled espresso machines, and so on. The extra size from using the standard iostreams comes from the wholesale inlining of template code, which makes it hard to fine-tune crucial operations without code bloat. For me, the build times are a more crucial problem because it means waiting longer and losing the "conversational flow" that is important in development.Then, consider debuggability. The template implementation of the standard string class is highly ingenious, but not pretty to the applications debugger. SHE IS Faced with compiler and debugger messages Using The Following Fully Expanded Name:

Class Std :: Basic_String , Class Std :: Allocator > And as for the Very Useful Map , I Leave It To Your Imagination. The name is so long that one gets dozens of compiler warnings about the internal name being truncated. std :: string should be as transparent as possible to the beginner, who should not be penalized for treating it as a built-in feature of the language. It is technically feasible to hunt for any defined typedefs in scope when putting out compiler error messages, and I may get around to doing that with the UnderC project. Verity Stob suggests writing a postprocessor to massage C error messages, and I hope she's joking. It is simpler just not to use such an overcomplicated type. My secret weapon in C development (which I'll confess here in public for the first time) is an interface-compatible string class that replaces the header in my larger projects. Occasionally, I'll rebuild with the standard headers to see MY Libraries Are Still Honest, But I Generally Let Other People Struggle with Being Correct At the Price of Performance.

Let me say that there are indeed applications that need the kind of flexibility offered by std :: string programs that manipulate both ASCII and Unicode strings or that need to customize the allocation strategy, etc. These are not generally very common (usually, programs either use ASCII or Unicode), and it seems unfair to burden programmers with the general machinery. It does make the library implementer's job more interesting, but it complicates that of the application programmers. This seems to be an inversion of the general principle that a well -designed library hides the difficulty of its implementation and is straightforward to use. that is, std :: string does not hide its implementation sufficiently because application programmers are constantly being made aware of it during their development process. and we can not assume that the users Of these classes is insists on a Particular Implementation Strategy, Which Goes Against The Idea of ​​Standards ideally specifying only the public interface and expected behavior of classes. Naturally, the general templates can always be available to those who genuinely need them.These considerations apply as well to the standard containers such as list <>, which have an extra default template argument that defines an allocator. Most people do not need this facility, although it's very useful if you do, of course. Again, these more general versions can be defined as separate templates. I appreciate that this makes the standard library less technically interesting, BUT LibRARIES MUST Be Primarily Designed with The end users in mind. To Rephrase The C Mantra, Users Should Not Be bothered by what they don't.

Apart from the danger of making things templates when they do not need to be templates, there is another problem that occurs with generic programming in C . Most people agree that the standard algorithms can be very useful. If I have a vector of integers, THEN SORT (V. Segin (), v.end ()) Will Sort It. Because The Comparison Is Inlined, this Generic Algorithm Will Be Faster Than the old-fashioned qsort (), AS Well AS Being Easier To Use, Especially IF THIS WAS A Vector OF A User-Defined Type. Copy () Will Copy Anything, Anywhere, And in The Most Efficient Way Possible.But Some Uses ArenenceSsarily Opaque:

COPY_IF (v.begin (), v.end (), ostream_iterator bind2nd (Greater (), 7)); To be pedantic, Each name shop be qualified with std ::, but assume that everything has been brought into the global namespace, either by separate using clauses or by some other unmentionable means) This example from Stroustrup1 can be more conventionally expressed in a form that makes it obvious that all the integers will run together on the output:

Vector Li; for (li = v.begin (); li! = v.eG (); Li) if (* li> 7) cout << * li; stroustrup tells US this explicit loops is "tedious and error-prone," but I can not see any advantages in using the first version Obviously, one can get used to this notation;. humans are very adaptable, and as professionals, we have to learn the new idiom. But it is certainly not less tedious, and it is arguably much less readable and flexible. Also, it can constrain design decisions. For example, given that we have a list of Shape * pointers, we can tell them all to draw themselves with either

For_each (ls.begin (), ls.end (), bind2nd (Mem_fun (& Shape :: Draw), Canvas); orshapelist :: item2000 li; for (Li = ls.begin (); li! = ls.end (); Li) -> Draw (Canvas); Now, if I want to modify this SO i online criteria (and specify don't want to put what behavior in't hpees themselves) , then I just have to add an if statement in the explicit loop. The only way I can think of doing this using the generic idiom is to actually define a function as the "payload" of the for_each () algorithm. Using the terminology of the software patterns book2, the first example is an internal iterator, and the second is an external iterator. The authors observe that C is not particularly good at internal iterators, and I think we should respect the limitations of the language. The problem here is THE OVERIC PARADIGM IN C - Again Leading to Unnecessary Difficulties. C Simply Does Not Do General Anonymous Functions Such As Lisp, Smallta LK, Ruby, etc. An Anonymous Function (or Lambda Expression) IN C Would Look Rather Like the Following3; Perhaps One Day Somebody Will Implement IT:

转载请注明原文地址:https://www.9cbs.com/read-291.html

New Post(0)