Comparison between C++/CLR generics and C + + templates

Source: Internet
Author: User
Tags definition comparison constant int size visual studio
Visual Studio 2005 introduces the type-parameter model of generic programming to Microsoft. NET Framework components. C++/CLI supports two types of parameter mechanisms-Common language runtime (CLR) generics and C + + templates. This article describes some of the differences between the two-especially the difference between a parameter list and a type constraint model.

Parameter list back again.

A parameter list is similar to a function's signal (signature): it identifies the number of parameters and the type of each parameter, and associates a unique identifier with each parameter so that within the template definition, each parameter can be uniquely referenced.

A parameter acts as a placeholder (placeholder) in the definition of a template or generic. The user establishes an object instance by providing an actual value bound to the parameter. Instantiating a parameterized type is not a simple text substitution (the macro extension mechanism is using text substitution). Instead, it binds the actual user value to the relevant formal parameter in the definition.

In generics, each parameter behaves as an object type or a type derived from object. As you can see later in this article, this constrains the type of action you might perform or the object declared by the type parameter. You can adjust these constraints by providing more explicit constraints. These explicit constraints refer to the base class or collection of interfaces that derive the actual type parameters.

In addition to supporting type parameters, templates also support expression and template parameters. In addition, the template also supports default parameter values. These are broken down by location rather than by name. Under both mechanisms, the type parameters are introduced together with the class or type name keyword.

Additional template features for parameter lists

Templates are added as type parameters, allowing two types of arguments: Non-type (non-type) parameters and template parameters. We will briefly introduce each of them.

Non-type parameters are constrained by constant expressions. We should immediately think of it as a numeric or string constant. For example, if you choose to provide a fixed size stack, you might specify both a non typed size parameter and an element type parameter, so that you can divide the class of the stack instance by the element class and size at the same time. For example, you can see a fixed-size stack with an untyped parameter in code 1.

Code 1: Stack with non-type fixed size

Template <class elemtype, int size>
Public ref class Tstack
{
Array<elemtype> ^m_stack;
int top;

Public
Tstack (): Top (0)
{m_stack = gcnew array<elemtype> (size);}
};

In addition, if the template Class Designer can specify a default value for each parameter, it may be more convenient to use. For example, setting the default size of the buffer to 1KB is good. Under the template mechanism, you can provide a default value for the parameter, as follows:

Template declaration with default values
Template <class elemtype, int size = 1024>
Public ref class Fixedsizestack {};
Users can overload the default size value by providing an explicit second value:

A stack of up to 128 string instances
fixedsizestate<string^, 128> ^tbs = gcnew fixedsizestack<string^, 128>;
Otherwise, because the second parameter is not supplied, it uses the associated default value, as follows:

A stack of up to 1024 string instances
fixedsizestack<string^> ^tbs = gcnew fixedsizestack<string^>;
Using the default parameter values is a basic design feature of the Standard Template Library (STL). For example, the following statement comes from the iso-c++ standard:

iso-c++ name Space STD example of default type parameter values
{
Template <class T, class Container = deque<t> >
Class queue;

Template <class T, class allocator = allocator<t> >
Class vector;
// ...
}

You can provide the default element type, as follows:

Template declaration with default element type
Template <class elemtype=string^, int size=1024>
Public ref class Tstack {};

It is difficult to prove its correctness from a design point of view because the container is generally not concentrated on a single default type.

Pointers can also be non typed arguments because the address of an object or function is known at compile time and is therefore a constant expression. For example, you might want to provide a third parameter for the Stack class that indicates the callback handler that is used when a particular condition is encountered. Using a typedef wisely can greatly simplify the statements that appear to be complex, as follows:

typedef void (*handler) (... array<object^>^);
Template <class elemtype, int size, handler cback >
Public ref class Tstack {};

Of course, you can provide a default value for the handler--in this case, the address of an existing method. For example, the following buffer declaration provides the size and handler:

void DefaultHandler (... array<object^>^) {...}

Template < class Elemtype,
int size = 1024,
Handler Cback = &defaulthandler >
Public ref class Tstack {};

Because the position order of the default values takes precedence over the naming order, you cannot provide an overloaded handler if you do not provide an explicit size value (even if the size is duplicated from the default). The following are the possible ways to modify the stack:

void demonstration ()
{
Default size and handlers
tstack<string^> ^ts1 = nullptr;
Default Handler
tstack<string^, 128> ^ts2 = gcnew tstack<string^, 128>;
Overload all three parameters
tstack<string^, ^ts3, &yourHandler>;
}

The second additional parameter supported by the template is the template template parameter-that is, the template parameter itself is represented as a template. For example:

Template Template Parameters
Template <template <class t> class arena, class arenatype>
Class Editor {
Arena<arenatype> M_arena;
// ...
};

The editor template class lists two template parameters arena and Arenatype. Arenatype is a template type parameter; You can pass integers, strings, custom types, and so on. Arena is a template template parameter. Any template class with a single template type parameter can be bound to arena. M_arena is a template class instance bound to the Arenatype template type parameter. For example:

Template Buffer Class
Template <class elemtype>
Public ref class Tbuffer {};

void F ()
{
Editor<tbuffer,string^> ^texteditor;
Editor<tbuffer,char> ^bliteditor;
// ...
}

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.