C ++ language style flow history

xiaoxiao2021-03-06  78

C language style flow history comes from: http://contextfree.Net/wangyg program code is also style, this is not a new thing. As early as the 1980s, C language programmers must be available between K & R-style and ANSI style. But I really have not seen any language like C , and it is so embarrassed and unpredictable in the code style: No one can not clear the C code can derive how many different styles, but I know, There are many C beginners when they face different styles of C code, often mistakenly see a few completely different programming languages ​​- this is enough to remind us that the evolution of C language style And the law of development is already a top priority.

And the cultivatists study the work of the cultural and intensive changes in the cultural and intensive changes, and there is no shortcut to study the history of C language style. We can only be based on the research methods of "the original" [1] advocated in the "Wen Xinjun" [1], follow the historical context, to find the source of the code style, to explore the internal cause of style evolution, and to clarify the technology The trend and law of development.

1. Belt-class C-pair of C language styles

In December 1983, Bjarne Stroustrup adopted Rick Mascitti's suggestion, before named "C " invented the new language, people have always used "C (c with classes" with classes to call this self-C language. "Dialect" with data abstract mechanisms. Although CPRE is essentially converted to traditional C language code (which is similar to the pre-processing process of the Pro * C language we see in Oracle, it is similar to the pre-processing process of the PRO * C language we see in Oracle. It does lay the foundation of all C code in style.

Class stack {char s [size]; char * min; char * TOP; char * max; void new (); public: void push (); char pop ();}; this "band-class C" code Recorded from the "C language design and evolution" of Stroustrup. New () in the code is actually a constructor of class Stack, which is very different from later C languages.

Obviously, the belt-class C is almost completely in the style of C language clothes. The statement in the code looks exactly in the C language, the structure of the Class is also roughly related to the structure of the Struct in the C language, which reflects the C language from C and keeps compatible with C and keeps compatible with C. this design idea. The rapid spread of C provides convenience (the smooth promotion of C languages ​​is obviously benefited from the existing huge users of C language), and also in the C language style is deeply filled with the C language imprint, so that After several years, when the C language has basically had "independent personality", Stroustrup has often reminded people to throw the C language thinking.

On the other hand, Stroustrup borrows from the SIMULA language, derived, access control, etc. Object-oriented concepts in the belt-class C. According to Stroustrup, he introduces an object-oriented mechanism for C language is to find a "appropriate tool" [2] to achieve distributed systems or solve similar complex problems. But in any case, Stroustrup has bundled the C language in fact for C language. It is not a Direct cause of C language style.

2. New image of I / O stream - C

If the birth of the C language is the C language and Simula language, then, in 1984, I / O streaming technology implemented by the operator is the C this young child opens the parent's shelter, to the new code style The first step is. Ostream & Operator << (Ostream & S, Const Complex & Z) {Return S << '(' << Z.Real () << ',' << Z.Imag () << ';} The above line of code comes from Stroustrup's sample program in "C Programming Language" in C Programming. Note that the line consists of "<<" connection code, I / O stream, variable, and character constants are cleverly together in the code. From a technical point of view, the introduction of this new grammar makes up for the weakness of the Printf () function family lack type security mechanism and extension capabilities in the C language. From the code style, "<<" and other plans have greatly changed the first impression of programmers to the C language. When I first contacted the C I / O circle, I had clearly felt that a C elves trying to get rid of the C language style is "<<" and ">>" stream "flowing" Come - This line of cloud-like code style has shown the determination and courage of C language in shaping new images and introducing new ideas.

3. OWL and MFC - style variation in window environment

At the end of the 1980s to the early 1990s, X Window, Mac OS, Windows and other window environments have appeared for programming, and C languages ​​balance the characteristics of object-oriented and traditional development methods undoubtedly become windows. The best choice for programming languages ​​in the environment. A group of C language-based window frames are not only successful in business, but also largely changed the style characteristics of the C language itself.

The earliest wins most programmatic C frameworks in window development is Borland's OBJECT Windows Library Framework library built into Borland C 3.1 in 1992. The following code takes from the sample program of Borland C 3.1:

class TGDIDemoWindow: public TMDIFrame {public: TGDIDemoWindow (LPSTR ATitle, LPSTR MenuName): TMDIFrame (ATitle, MenuName) {}; virtual void SetupWindow (); virtual void ArtyDemo (TMessage &) = [CM_FIRST ArtyDemoID]; virtual void Quit (TMessage & ) = [CM_FIRST QuitID]; virtual void WMTimer (TMessage &) = [WM_FIRST WM_TIMER]; virtual void WMDestroy (TMessage &) = [WM_FIRST WM_DESTROY];}; in order to solve the key message mapping problem window programming, OWL's The designer introduced "= [...]"'s quiritic grammar for the members of the C language, which is a language style that many of the programmers who have used Borland C have not been forgetful. I acknowledge that Borland has provided us with the best compiler and the best integrated development environment (IDE) in the early days of C language, but Borland's alternative language style introduced by the OWL frame is indeed. Objectively speaking, this account should not be budget on the Borland header, because OWL's predecessor is Borland's frame code purchased by White Water.). Just two years later in the Borland C 3.1, Microsoft has gradually recaptured the leading power of the Windows Development Tool market with its hegemony and famous Visual C series products. Unlike Borland, the MFC (Microsoft Foundation Class) frame library in Visual C does not give it to OWL, but uses the following way to implement message mapping (code taken from MSDN sample program):

// Example for BEGIN_MESSAGE_MAPBEGIN_MESSAGE_MAP (CMyWindow, CFrameWnd) ON_WM_PAINT () ON_COMMAND (IDM_ABOUT, OnAbout) END_MESSAGE_MAP () In fact, the MFC framework written in C code while extensive use of macros and other pre-compiler directive, also under a common platform WIN32 Hungarian style (writing specifications for identifier case and prefix) have played the limit. This doesn't need to have a multikrate tongue, and many programmers can determine whether the MFC framework is used in the code from the case's case's case.

Unfortunately, the language style of MFC is C does not get the first C expert. For example, many scholars including Stroustrup suggest that we use less or even macro definitions such as pretreatment instructions. At this point, the MFC's approach is clearly made by the experts' discipline. It should be said that Microsoft's domineering has created a huge success of MFC; however, from a pure linguistic perspective, MFC's contribution to language style is far less than its contribution to window framework technology.

4. Template - the foundation of modern C style

Stroustrup has announced the syntax design related to template (Template) in 1988. There is no doubt that this is a technological improvement in the fact that modern C 's language style. The concept of template comes from the CLU language and integrates the advantages of related technologies in SmallTalk and ADA languages. After 1991, the development environment of the template mechanism (DEC C , IBM C , Borland C , etc.) was introduced. But until the 1995 STL (Standard Template Library) template library gradually developed mature, template technology quickly spread in programmers. The following example is taken from the sample code of the SGI STL, which basically reflects the overall style of the C code after using the template technology:

template InputIterator find (InputIterator first, InputIterator last, const T & value) {while (!! first = last && * first = value) first; return first;} This C code in, In addition to a few keywords and operators, we can almost find the traces of the C language. Template technology has a dual demand for type safety and coding flexibility, but it also introduces a class-style, but also less difficult (relative to code without template). Many traditional C language advocates hate this style of code, but more new generation programmers have a good time. In 1998, STL was incorporated into C international standards as part of the standard C library (STANDARD C Library) with the support of the ANSI / ISO Standardization Commission. Today, the language style represented by modern C technology such as template and abnormalities has also been the "official style" of the C world in fact.

5. ATL - Alternative C in the COM

In addition to the STL template library, there is an example of template style. The following codes are broken from Visual C automatically generated ATL control project:

class ATL_NO_VTABLE CMyATLObj: public IMyATLObj, public IpersistStreamInitImpl , public IOleControlImpl , public IOleObjectImpl , public IoleInPlaceActiveObjectImpl , public IViewObjectExImpl , public IoleInPlaceObjectWindowlessImpl , public IPersistStorageImpl , public IspecifyPropertyPagesImpl , public IQuickActivateImpl , public IDataObjectImpl , public IProvideClassInfo2Impl <& __ uuidof (CMyATLObj), NULL>, public CComControl ...... Note CMyATLObj control class code, CMyATLObj class actually From the N interface classes and control classes, the category of the category can be seen everywhere - this is Microsoft's unique ATL style code for our design. The reason why the template, multiple inherits such as template, multiple inheritance, etc., mainly in order to adapt to COM, OLE, ActiveX, etc., the relatively complex technical system is originally in the architecture. But in this way, using the ATL code in all C code, there is a unusual length of the problem: all are angular brackets, everywhere is the identifier of "I", and even multiple angle brackets Nested ... If a programmer who is just learning C language immediately reads a large paragraph ATL code, I think, I can't take a few minutes, he will be in the code in the code, and the bizarre language style has a spirit of collapse. . 6. Standard C - a new language?

The standardization process of the C language is far behind the popularity of the language itself. After 1990, the ANSI / ISO C Standardization Committee will also convene a large number of experts including Stroustrup, and a well-known company including Apple, Borland, Dec, HP, IBM, Microsoft, Sun, and Unisys, like all countries. The Parliament or People's Congress is the same, discussing and voting, developing C international standards. The standard is not officially released until September 1998. In the archives of the International Standardization, the C standard code is ISO / IEC 14882: 1998.

Stroustrup recommends that we use standard C as a brand new language [3]. This statement is obviously based on the fact that the standard C language already has a stable, can promote the language style, that is, through the existing affirmation of STL, the ANSI / ISO Committee officially officially It is recognized that modern C style including typical language characteristics such as templates, containers, I / O streams, and exception handling. Style stability means the progress and maturation of the language itself, which means that programmers must rise to a new level to a new level - those still writing only C code consisting only by class and C language library functions Members will definitely become Stroustrup and their colleagues. Stroustrup 's "C Programming Language" 3rd edition made the most authoritative interpretation of the standard C style. Under the call of experts and scholars such as Stroustrup, more and more projects begin to write code that meets standard C style. This is particularly obvious in many projects of many open source. More than half of this is due to the use of C language open source projects, not like project groups in large enterprises, in terms of language style, will be accepted by the company background or historical habits. In the specific programming practice, the programmers of the open source project can resolutely implement the standard C language style, and on the other hand, it can add some emotional colors according to their own preferences. For example, in the source code of OpenOffice, the prefix specification of the identifier is quite characterized, and the variables of the pointer and the reference type are distinguished by different prefix alphabets; the source code segment of the Linux desktop manager KDE 3.1.4 given below Show out, programmers developing KDE are more or less affected by Java language style in the code style:

class delUser: public KDialogBase {Q_OBJECTpublic: delUser (KUser * AUser, QWidget * parent = 0, const char * name = 0); bool getDeleteHomeDir () {return m_deleteHomeDir-> isChecked ();} bool getDeleteMailBox () {return m_deleteMailBox- > ischecked ();} private: qcheckbox * m_deletehomedir; qcheckbox * m_deletemailbox;}; 7. Read code - compatible with language style

Speaking of standard C language style, it is necessary to see a very quirky but very interesting code for everyone. Do you understand this C code below? Is it true C code?

%: include using namespace std;%: defline max 5void main () <% int m <: max:>; int i = 1; for (i = 0; i = i; if (i not_eq 3 and i <5) cout << i << endl;%>%> This is a piece of code I have written I. You may not run it in the Visual C environment, but its syntax is indeed in accordance with the 1998 C standard regulations. In the GNU C environment, I successfully compiled it into an executable program.

Simply put, this style of strange C code is actually written in accordance with the provisions of the Alternative TOKENS in C standards. The design of the specified design is to adapt to the status quo of the standard character set in some countries in Europe lack of "{", "#" equivalence (especially in some traditional terminal devices). Strictly speaking, this must not be a real language style, but similar provisions have indeed embodied the ANSI / ISO committee in a wide chest, which is compatible with the language design. 8. C Builder - Borland's revival road

After released Borland C 3.1, Borland was issued to Microsoft [4] because it was not considerable to take the market of C development tools. After experiencing the failure of Borland C 4.0, 4.0, 4.0, 4.0, 4.0, in 1997, Borland launched a new C development tool C Builder. This product saved in Borland in the market not only in the interface style, not only in the interface style. Delphi is not matte, even directly moving Delphi's VCL (Visual Component Library) library. As a result, the code developed using C Builder is born to the Delphi style infection, the length of the PASCAL language (the following code takes from the sample code of C Builder 6.0):

class TFormClrDlg: public TForm {published: // IDE-managed Components TColorDialog * ColorDialog; TButton * Button; TPanel * Panel1; void fastcall ButtonClick (TObject * Sender); private: // User declarationspublic: // User declarations virtual fastcall TFormClrDlg ( Tcomponent * Owner;}; To be honest, although C Builder is good in the market, I still don't like Borland to deliberately confuse the C language with the Object Pascal language in Delphi. Behind Borland's approach, there is a reason to improve product versatility, shorten product development cycles, but the code developed using C Builder has been getting farther and farther away from the standard C style on the outside.

It is worth noting that Borland launched its next-generation C development tool in 2003 - C Builderx. What a smile is that this time Borland actually constructed the C development environment on the PrimeTime platform implemented by Java language. This less will push C language to an extremely embarrassing situation. However, C Builder has brought us some good news: In the subsequent version, C BuilderX will integrate vxwindows framework libraries [5], which is obviously compared to the use of VCL under this framework. The code has more standard C style.

9. Visual C .NET - Revolution or rebellion?

Microsoft introduces C into the .NET environment is actually radical than Borland. Single from the style, the code developed using Visual C .NET may have many different styles such as MFC, ATL, standard C , Net managed code. Among them, the maximum impact on the C language itself is, of course, several fresh bloods that need to be injected into c : #using

Using namespace system :: reflection; using namespace system :: security :: permissions

Public __Value Enum numberuff {E1 = 1, E17 = 17};

[Attribute (AttributeTargets :: Class, AllowMultiple = true)] public __gc class abc {public: ABC (INT __GC []) {} ABC () {} ABC (INT) {} ABC (INT, FLOAT) {} ABC ( Somestuff) {} ABC (String *) {} int RGNFIELD __GC []; double rgdfield __gc []; double dfield;}; the code from the sample program from the MSDN. Seeing new features and new features such as Microsoft Big knife, attribute, Attribute attribute, etc., see .NET hosting code, new singularity, nearly retractable, I don't know, I should be Microsoft on the development of general language platforms. Work hard, or should be c in C # language shadows, the roots of the .NET big play is discouraged. Perhaps, the feelings of language and programmers are in Microsoft's eyes, they are small things that are not worth mentioning, and they can compare with .NET's magnificent strategy and Microsoft's strong empire?

10. Review and prospect

The change of language style reflects the evidence of technical ideas and industry demand from one side. Since 1979 Stroustrup completed the first CPRE pre-procedure, C language came to this world that was full of 25 years. This is a language that is born, growing and developing in practice. Perhaps, Stroustrup did not want to design it as a pure object-oriented language like SmallTalk from the beginning. The demand for openness, high efficiency, compatibility, and scalability causes C language to form a typical multiParadigm language. Whether it is C for the SIMULA language inheritance, or later, the use of languages ​​such as SmallTalk, Ada, Clu, whether it is the rapid spread of the ANSI / ISO standard style, or the unremitting efforts of Visual C .NET in technology innovation, all of these history Changes have indicated that the diversity of C is mainly derived from the C language itself "Haina Baichuan" chest and gas.

5 years later, when C stepped into the year, will it bring us new surprises? Will we also see a newcoming C language style? Maybe, no one can give an accurate answer. But as a programmer, we should at least know: No matter what kind of software needs, no matter what kind of thinking, C language gives us the biggest freedom in the language style; when we really understand the spirit of C language After that, this freedom will also become a solid foundation for all excellent software and elegant code. references

[1] Liu Wei. Wen Xin Dragon Dragon.

[2] Stroustrup B. C Design and Evolution. Beijing: Machinery Industry Press, 2002

[3] Stroustrup B. Learning Standard C as a new language. C / C Users journal. PP 43-54. May 1999.

[4] Li Wei. Borland Legend. Beijing: Electronic Industry Press, 2003

[5] Li Wei. The details of Borland C Builderx. Programmer. 2003.11.

[Wang Wei Gang, November 2003]

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

New Post(0)