Template Specialization

xiaoxiao2021-03-06  54

See this in msdn library

Page Options

Conformance to Iso Standards for C

Kate GregoryGregory Consulting April 2004 Applies to: Microsoft® Visual C ® .NET 2003Microsoft® Visual C ® Toolkit 2003Microsoft® Visual Studio® .NET Summary: Demonstrates how Visual C can be used to incorporate the .NET Framework into C applications (5 printed. pages) This article is part of a code sample included with the Visual C Toolkit 2003, available for download at http://msdn.microsoft.com/visualc/vctoolkit2003. Contents Template SpecializationThe sampleCompiling and runningConclusionRelated Books Microsoft® Visual C ® .NET 2003 achieves the highest level of standards-conformation of any release of Visual C . Visual C was first launched in 1993, and the ISO C standard dates from 1997. Each release of Visual C since the standard has become more compliant, and this release achieves Roughly 98% Compliance. (No Single Industry Accepted Standardized Test-Suite Exists COMPARE OR Measure C Compilementations. Three Popular Suites Are Dinkumware, Perennial, An AN d Plum Hall. Visual C .NET 2003 was tested against all three). This high level of compliance enables Visual C .NET 2003 to compile popular modern C libraries (including LOKI, BOOST, and BLITZ), a feat achieved by few, if Any, Other Compilers. A Vital Standard Language Feature For Compiling these Libraries IS Partial Template Specialization, Which is Demonstrated by this sample; try compiling it with other compilers; Visual C

2002, for example, gives error messages and can not compile it. Template Specialization Templates typically define a class or function using one or more placeholders that represent classes or types. At compile time, instantiations of the templates are generated to match the use of the template In Your Code. for Example, this Template Function Can Be Used to Compare Integers, Floating Point Numbers, or Instances of any Class That Defines the> Operator: Templatet Biggest (T A, T B)

{

IF (a> b) Return A;

Return B;

}

You might use this template like this: int x = biggest (3, 4);

Double D = Biggest (4.1, -2.3);

// SalesRep Instances Created Elsewhere,

// Operator> Defined for Salesrep Class

Salesrep Bestseller = Biggest (John, Jane);

The compiler will generate code for biggest (int, int), biggest (double, double), and biggest (SalesRep, SalesRep), and this code will be linked into your executable. C programmers traditionally run into trouble with templates that need to handle strings. Traditional char * strings can not be compared with> and <(those operators just compare the pointer values, not the strings to which they point), or copied with = the way numbers and objects can. One solution is to write a Specialization-An Instantiation for a Particular Type That The Compiler Will Use Instead of The More General Template. here is a specialization of biggest () for the char * type: template <>

Const Char * Biggest (Const Char * A, Const Char * B)

{

IF (strcmp (a, b)> 0) Return A;

Return B;

}

Visual C has been able to handle template specialization for a long time. What's new in the 2003 release is the ability to handle partial template specialization. This applies to templates that take two placeholder types, rather than just one as biggest () does. The Sample Collection classes are especially likely to use multiple placeholders. A lookup table might hold values ​​(numbers, Employee instances, dates, char * strings) that are indexed by a key that is an integer, a char * string, or a date. The Pair Template Is A Simple Class That Works with Two Placeholders: TemplateClass Pair

{

Private:

A index;

B Value;

PUBLIC:

Pair (a aa, b bb): Index (aa), value (bb) {}

Void Display () {cout << index << '<< value << endl;}

Bool Operator> (Const Pair

& p) {Return INDEX> P.index;}

}

Pair does not do much, but you can imagine that similar code would be at the heart of a flexible collection solution. It holds copies of the index and value, displays them, and can compare two Pair instances by comparing only their indexes. It works flawlessly when the index type, A, is an integer or other numeric type, or a class that has implemented operator>. When the index type is char *, the comparisons become meaningless since> compare the numerical address of the character pointer rather than the characters to which it points. Additionally, the initialization of index will not make a copy of the characters, but only of the pointer. A partial template specialization is a specialization where one of the placeholders has been replaced with a specific type (char * In this case) But The Other Has Not. For Pair, a Partial Specialization for Char * Index Values ​​Looks Like this: TemplateClass PAIR

{

Private:

CHAR * INDEX;

B Value;

PUBLIC:

Pair (Char * aa, b bb): value (bb) {index = new char [strlen (aa)];

STRCPY (INDEX, AA);

Void Display () {cout << index << '<< value << endl;}

Bool Operator> (Const Pair

& P)

{RETURN (Strcmp (INDEX, P.INDEX)> 0);

}

This code would not have compiled under earlier versions of Visual C From Visual C .NET 2003 onward, however, it compiles Compiling and running The main () in this sample creates various Pairs and compares them:.. Int main (int argc, char * argv [])

{

Pair

First (2.2, 3);

First.display ();

Pair

Second (2.1, 4);

Second.display ();

IF (first> second)

COUT << "first is well" << endl;

Else

COUT << "first is not well" << endl;

Pair

Third ("Hello", 4); third.display ();

Pair

Fourth ("World", 5);

Fourth.display ();

IF (Third> Four

Cout << "third is well" << Endl;

Else

COUT << "Third is not well" << endl;

Return 0;

}

To Compile The Sample: Cl / EHSC Conformance.cpppp

To Run IT: Conformance

You Should See this Output: 2.2 3

2.1 4

First Is Greater

Hello 4

World 5

Third Is Not Greater

The Pair called first is greater because 2.2 is greater than 3. The Pair called third is not greater because "Hello" is not greater than "World". Conclusion Partial template specialization is a vital technique for writing rich and useful collections. It can serve a useful purpose in many C programs, and is just one of the many new areas of standards conformance in Visual C .NET 2003. For more information about Visual C ISO C standards conformance see Visual C .NET 2003 Enhanced Compiler Conformance. Related Books Microsoft Visual C .NET 2003 Kick Start by Kate Gregory?

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

New Post(0)