Pure C ++ column...

Source: Internet
Author: User

Pure C ++ column...

Original: Stanley B. Lippman

Translation: Coffer

Source: pure C ++: Hello, C ++/CLI

Welcome to my first column hosted in msdn magazine! In fact, I have written the first column before, and its content is completely different-it is about the new generic programming support in Visual Studio 2005. But in retrospect, I think that articleArticleToo many unsolved questions are left. Therefore, I opened this column to provide a team with an overview of C ++/CLI in Visual C ++ 2005. As a result, I will write some columns about. Net generic programming. Don't forget that this column is based on the preview version, so some content may change.

What is C ++/CLI?

C ++/CLI depicts a multi-element combination. The C ++ here is of course the C ++ invented by Bjarne stroustrup In the Bell lab. Programming Language . It supports static object models with optimized speed and execution file size. However, apart from heap memory allocation, it does not support Runtime Program Changes to the object. It allows unlimited access to the underlying machine, but its access capability is very limited or impossible for the activity type in the running program and the basic structure of the related program. My colleague herb Sutter from Microsoft, who is also the main architect of C ++/CLI, thinks C ++ is a concrete language.

"CLI" is a multi-layer architecture that supports dynamic component programming models. In many ways, the object model it represents is exactly the opposite of that in C ++. It is a runtime software layer, a virtual execution system that runs between the application and the underlying operating system. Access to the underlying machine is strictly restricted. Allows you to access, discover, and construct activity types of running programs and basic structures of associated programs. The slash "/" indicates a binding between C ++ and cli. Details about this binding constitute the general topic of this column.

Therefore, for "What is C ++/CLI ?" The first recent answer to this question is: it is a binding from the static C ++ object model to the dynamic CLI component object model. In short, it is how you use C ++. instead of using C # Or Visual Basic.. net. Like C # And CLI itself, C ++/CLI is being standardized by ECMA (European Computer Manufacturers Association) and eventually certified by ISO.

The Common Language Runtime (CLR) is a Microsoft-version CLI dedicated for Windows operating systems. Similarly, Visual C ++ 2005 is the implementation of C ++/CLI.

The second approximate answer is: I THINK C ++/CLI is integrated in C ++. the net programming model is the same as the former Bell lab's integration of generic programming with Templates in C ++ at that time. In either case Code Investment in the database and your existing C ++ expertise are protected. This is a basic requirement for C ++/CLI design.

Learn C ++/CLI

The design of a C ++/CLI language has three layers. These three layers are also applicable to all languages: ing of the language layer syntax to the common type System (CTS; select the details of the underlying CLI basic organizational structure exposed for direct operations by the programmer, and select additional features that are directly supported by the CLI to be provided.

The first layer is that all CLI languages are common to a large extent. The second and third layers are the differences between a CLI language and other languages. Based on the problem to be solved, you can select a language or combine multiple CLI languages. To learn the C ++/CLI language, you must master these three design layers.

How to map C ++/CLI to CTS?

Understanding the underlying CTS is very helpful for learning C ++/CLI. It mainly includes three common class types:

    • Polymorphism reference type, which is used for all class inheritance;
    • Non-polymorphism value type, which is used to implement the specific type that requires the running timeliness rate, such as number type;
    • Abstract interface type, which is used to define a public operation set that is used by a group of reference types or value types that implement this interface;

In terms of design, although the CTS ing between CTS and a set of built-in language types is common for all CLI languages, the syntax of each CLI language is different. For example, in C #, we can define an abstract base class shape to derive a specific geometric model object from this class.

 
Abstract class shape {...} // C #

In C ++/CLI, you can write as follows to indicate identical underlying reference types:

 
Ref class shape abstract {...}; // C ++/CLI

In the underlying il (intermediate language), the above two statements are exactly the same. Similarly, in C #, we can use the following code to define a specific point2d class:

 
Struct point2d {...} // C #

In C ++/CLI, write:

 
Value class point2d {...}; // C ++/CLI

The class family supported by C ++/CLI represents a local mode of CTS integration. It determines your syntax selection, for example:

 
Class native {}; value Class V {}; ref class R {}; Interface Class I {};

CTS also supports enumeration class types, which are slightly different from local enumeration. c ++/CLI supports both of them:

 
Enum native {fail, pass}; Enum class clienum: Char {fail, pass };

Similarly, CTS supports its own array type, and its behavior is also different from the local array type. Microsoft also provides support for the two types:

 
Int native [] = {,}; array <int> ^ managed = };

It is not accurate to think that any CLI language is closer or almost the ing to the underlying CLI than the other language. On the contrary, each CLI language only expresses its own opinion on the underlying CLI object model. In the next section, you will see this more clearly.

Detailed CLI standards

The second level that must be considered when designing the CLI language is to integrate the underlying CLI implementation model into the language. What problems does this language solve? What tools are required to solve these problems? What kind of programmers are likely to be attracted by this language?

Next, we use the value type issue that occurs in the hosting heap. In many cases, the value type can be found in the managed heap:

    • Using the implicit box-in/box-out operation (boxing) -- when an instance of the value type is assigned to an object, or a virtual method is called through a value type that has not been rewritten;
    • When the value type is used as a member of the Reference class type;
    • When the value type is stored as a CLI array element;

Whether programmers are allowed to handle this type of value address is a problem that must be addressed when designing the CLI language.

Problems?

Any objects in the managed heap may be reassigned during the Garbage Collector's cleaning and shrinking process. Any pointer to these objects must be tracked and updated at runtime, programmers cannot manually track them by themselves. Therefore, if you are allowed to use a value type address that may be in the managed heap, you also need a tracking pointer in addition to the local pointer.

How can we weigh them? On the one hand, you need to consider simplicity and security. Directly introducing support for one or more tracing pointers will make the language more complex. If such support is not provided, the number of programmers that can be found increases as the complexity required decreases. In addition, allowing programmers to access these short-lived value types increases the possibility of programmer errors. She intentionally or inadvertently performs some dangerous actions on the memory. Tracking pointers are not supported, so you can create a safer runtime environment.

On the other hand, efficiency and flexibility must be considered. Each time a value type is assigned to the same object, a new box-in/box-out operation is performed for this value. Allows access to the value type of this box-in/box-out operation, and allows the update operation in the memory, which may provide important performance improvements. Without some form of tracing pointer, you will not be able to use the pointerAlgorithmTraversing the CLI array means that the CLI array cannot be integrated into the iterator mode in STL (Standard Template Library), nor can it work together with generic algorithms. The allowed access to the box-in/box-out value type will greatly improve the design flexibility.

In C ++/CLI, Microsoft chose to provide a series of addressing modes that process value types in the managed heap:

Int ival = 1024; int ^ boxedi = ival; array <int> ^ iA = gcnew array <int> }; interior_ptr <int> begin = & Ia [0]; value struct smallint {int m_ival ;...} si; pin_ptr <int> PPI = & Si. m_ival;

A typical C ++/CLI developer is an experienced system programmer who provides the underlying architecture and core applications as the basis to build the future. She must solve problems related to scalability and performance and view the underlying CLI at the system level. The detailed standards of a CLI language reflect the appearance of its programmers.

Complexity itself is not a denial of quality. Human life is much more complex than a single cell. This is certainly not a bad thing. However, when the expression of a single concept becomes more complicated, this is often considered a bad thing. In C ++/CLI, the CLI development team has tried to provide an elegant way to express a complex subject.

Additional features

The third design layer is that the features at the specific language layer are more than those directly supported by the CLI. In this way, a ing between the language Layer Support and the CLI underlying implementation model needs to be established. In some cases, this is not possible because the language cannot mediate CLI behavior. The following is an example of solving virtual functions in constructors and destructor of the base class. To reflect the ISO-C ++ semantics in this case, you need to reschedule the virtual table in the constructor and destructor of each base class. This is not possible because virtual table operations are hosted at runtime rather than in a separate language.

Therefore, this design layer is a compromise between superiority and feasibility. C ++/CLI provides three additional functions:

    • Resource Acquisition is in the form of initialization (raiI), especially deterministic finalization for the garbage collection type called occupying rare resources) provides an automated mechanism;
    • The deep copy semantics related to the c ++ copy constructor and the copy assignment operator, but it cannot be extended to the value type;
    • In addition to the CLI generic mechanism-this was originally the topic of my first column, and also provided direct support for the c ++ template for the CTS type. In addition, STL verifiable versions for the CLI type are also provided;

Let's look at a simple example: deterministic termination. If there is a Finalize method related to the object before the memory associated with the object is recycled by the garbage collector, this method will be called. You can regard this method as a super destructor, because it does not depend on the life cycle of the object program, it is called termination. The time when the Finalize method is called, or even whether to call it is undefined. This is the meaning of the uncertain termination operation of the garbage collector.

Uncertain termination can work effectively during dynamic memory management. When the available memory space is seriously insufficient, the garbage collector will play a role and solve the problem. However, when an object involves some important resources, such as database connections, some types of locks, and local heap memory, the uncertain termination performance is unsatisfactory. In this case, it is best to release unnecessary resources as soon as possible. Currently, the CLI uses the following solution: a class releases resources in the dispose method of its idisposable interface. The problem here is that dispose needs to be explicitly called, so it cannot be executed.

The basic design mode of C ++ is the aforementioned resource acquisition (initialization), which means that the class Obtains resources through the constructor, release resources by using destructor. It is automatically managed during the lifetime of the Class Object.

The following describes how to release a resource of the reference type:

    • Use destructor to compress the Code required during resource release;
    • Automatically calls the Destructor bound to the lifetime of the Class Object;

In CLI, classes of reference types do not have the concept of class destructor. Therefore, if the Destructor is mapped to another thing in the underlying implementation, the compiler completes the following internal conversions:

    • The class has its base class list, inherited from the interface idisposable extension;
    • The Destructor is converted to the idisposable dispose method;

This is only half done, and an automatic method of calling the Destructor is also required. Stack-based symbols for reference types are supported, that is, their lifecycles are associated with their declared scopes. The compiler internally converts symbols and assigns reference objects in the managed heap. With the termination of the range, the compiler inserts a call to the dispose method-a user-defined destructor. The actual memory associated with the object is still under the control of the garbage collector. See figure 1.

C ++/CLI is not just an extension from C ++ to the management world. On the contrary, it represents a complete programming example, similar to the integration of early multi-inheritance and generic programming examples into the language, I think this team has done an outstanding job.

So what do you think of C ++/CLI?

C ++/CLI represents the synthesis of local and hosted programming. In this repetition process, this synthesis is achieved through an independent and equivalent source code level and binary element community, this includes the mixed mode (source code-level mixing of local and CTS types, and binary mixing of local and CER object files), and the mixture of local and CTS types, added a new binary file for mixing local objects and the pencil object), pure mode (source code-level mixing of local and CTS types, all the locally compiled pencil object files ), local class (you can control the CTS type only through a special packaging class), and CTS class (you can only control the local type as a pointer ).

Of course, C ++/CLI programmers can also choose to program by using the CLI type separately. In this way, provide verifiable code that can be hosted, such as the stored procedure in SQL Server 2005.

Now, back to the question of C ++/CLI, it is to enter. NET programming model first threshold, with C ++/CLI, you not only have the ability to migrate C ++Source codeLibrary path, and the ability to migrate C ++ expertise. This makes me feel very comfortable.

Please send questions and suggestions to purecpp@microsoft.com.

Author Profile

Stanley B. LippmanIs an architect of Microsoft Visual C ++ team. He has been studying C ++ in Bell Labs since 1984 with the inventor Bjarne stroustrup of C ++. During this period, he made featured animations at Disney and DreamWorks, as well as senior consultants of JPL and software technology directors at Fantasia 2000.

This article is from the msdn magazine February 2005 journal and can be obtained through a local newsstand, or preferably subscribed

This article is translated by the vckbase MTT team
author blog: http://blog.csdn.net/oyljerry/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.