Stl.net exploration of combination of generics and template

Source: Internet
Author: User
Tags bool include integer reference
Before the template starts, first of all, when you ask a C + + programmer How to complete a given task, he or she may provide a dozen or more list of potential solutions, but it will be extremely detailed to identify the problem of each scenario, leaving you at a loss, difficult to choose. Visual C + + 2005, through the C++/cli language, introduces the concept of generics, so that C + + programmers can further improve the development efficiency of the program. About. NET generics and C + + templates, although syntactically similar, generics and templates are implemented in completely different ways without any built-in compatibility.

When it comes to the difference between generics and templates, believe in each. NET Platform, C + + programmers will ask this question: what kind of technology should I choose? Those who have been developing in C + + for several years (especially in Visual C + +), I believe I already know the answer: both technologies have excellent features, but any one is not a superset of another one, which technology is limited to a given task, in short, there is no technology that can be applied to all scenarios. Similarly, this dilemma has afflicted Visual C + + programmers for years: Win32 or MFC, ATL or WTL, COM, or C-style DLLs, #import或是CComPtr.

   involved. NET development of C + + knowledge

In the past, Visual C + + used certain techniques to work together the collections in the STL with other technologies, such as the templated class called Ccomenuonstl in the Active Template Library (ATL), which allows a Visual Basic client to enumerate the C + + The contents of the STL collection provided by the COM server. Although this level of integration is very shallow, it is very useful to be proven in many limited cases. Based on the same theme, STL. NET provides C + + programmers with an extension of the standard STL library, which allows an STL set to be used within a C++/CLI assembly to be exposed as a generic collection. NET assemblies.

Stl. NET provides the same interface as the standard STL collection, so there is no learning curve for C + + programmers who are familiar with standard STL sets and algorithms. With Visual C + + 2005来 as a description, STL. NET header file is located in the Cliext directory, if you want to use a Stl.net collection, such as vector, you must include the <cliext/vector> instead of the standard <vector> NET collection is contained in the Cliext namespace, not the Std namespace used by the standard STL collection.

Note: the Visual C + + development team is still working hard to make stl.net easier to use and to improve its performance. Because Stl.net is still constantly making new improvements, this article will not explore the depths of the Stl.net collection, but from a higher point of view, why Stl.net will do a great service.

   bridging generics and templates

Bridging generics and templates these two different worlds are a daunting task. A template is only a C + + concept and exists only in the compile time, however, generics are one. NET concept, which exists in a compiled assembly, and for all. NET language is available. Stl. NET, the solution is to use the class set as a C + + template class, as well as the C + + template class. NET reference type, and it implements the ICollection generic interface. The vector declaration in STL and Stl.net illustrates this design:

STL Vector Declaration
Template<class _ty, class _ax = allocator<_ty> >
Class vector;

Stl.net Vector Statement
Template<typename _value_t>
Ref class Vector:generic::icollection<_value_t>


In the declaration of both, there are some key differences, in addition to the implementation of the generic interface, stl.net vector does not specify an assignment operator, simply call gcnew to allocate a new required element. Stl. NET collection declaration uses the REF keyword, which means that they are all. NET reference type, which will be assigned to the. NET managed heap.

   using Stl.net

To remove some of the differences between stl.net set declarations (which are related to syntactic differences between C++/CLI and standard C + +), using the stl.net set is essentially exactly the same as the STL set. The following console sample program declares a vector object, and then adds some other different types of elements to the collection:

#include "stdafx.h"
#include <cliext/vector>

using namespace System;
using namespace Cliext;

int main (array<system::string ^> ^args)
{
vector<object^>^ v = gcnew vector<object^>;
V->push_back (nullptr); The first element is empty
V->push_back (gcnew Object ()); The second element is a pure object
V->push_back (1); The third element is a boxed integer
V->push_back ("Element Four"); The fourth element is a string
return 0;
}
The above code illustrates an important feature of stl.net: It's still a very familiar collection class, and another feature is that you can use two different programming models in one collection, which means that if you want to find the integer 1 in the code above, you can use the following STL finding algorithm:

Use as an STL collection
BOOL Containsonestl = Find (V->begin (), V->end (), 1)!= v->end ();
Other than that. NET ICollection generic interfaces can also be used to implement the same logic:

As. NET collection uses
BOOL Containsonedotnet = v->contains (1);
This can be used in the same collection class with both STL and. NET algorithm-without duplicating content or providing bridging functions-allows C++/CLI programmers to select the most appropriate functions and libraries for any operation on the set.

The last feature about Stl.net is that it can be written for C # or vb.net. NET assembly, providing its Stl.net collection seamlessly. Because the Stl.net collection implements the ICollection generic interface, there is no loss on type safety, and the Stl.net collection uses managed memory to store the elements in the collection, and therefore, when interoperating with the C++/CLI assemblies that provide the Stl.net collection, No further loss of performance or code security.

   . NET ticket for C + + programmers in the Kingdom

Stl. NET represents an important part of the Visual C + + product that allows C + + programmers to take advantage of their existing skills and experience, and to use the class libraries that are brought together with powerful collections and algorithms as soon as possible without being rapidly growing. NET world shut out.

Stl. NET as a bright start, will help C + + in maintaining the glorious tradition of language, and become. NET language development of the first kind of choice.

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.