Page OptionsKate Gregory April 2004 Applies to: 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. ContentsTemplate Specialization 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 to compare or measure C++ compiler implementations. Three popular suites are Dinkumware, Perennial, and 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 specializtion, which is demonstrated by this sample. Try compiling it with other compilers; Visual C++ 2002, for example, gives error messages and cannot compile it. Template SpecializationTemplates 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: template
T 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't 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 SampleCollection 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: template
class 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 doesn't 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: template
class 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 runningThe 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 greater" << endl;
else
cout << "first is not greater" << endl;
Pair
third("Hello",4);
third.display();
Pair
fourth("World",5);
fourth.display();
if (third > fourth)
cout << "third is greater" << endl;
else
cout << "third is not greater" << endl;
return 0;
}
To compile the sample, use this command line: cl /EHsc conformance.cpp 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". ConclusionPartial 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 BooksMicrosoft Visual C++ .Net 2003 Kick Start by Kate Gregory ? |
相关推荐
explicit specialization of non-template
相信很多人在使用C++模板特例化都遇到过这个错误,很多人不知道原因甚至一头雾水,接下来我就用这篇文章解释下出现这个错误的原因。模板的所有特例化版本都应声明在与主模板相同的名字空间中。如果某个特例化版本被使用,而且它是显式声明的,则它必须显式定义。换句话说,显式特例化一个模板就意味着编译器不再为此特例化版本生成其它定义。
模板特殊化(Template specialization)
http://hi.baidu.com/yzmforever123/item/5ce760521239219408be17a4 模板的特殊化是当模板中的pattern有确定的类型时,模板有一个具体的实现。例如假设我们的类模板pair 包含一个取模计算(module operation)的函数,而我们希望这个函数只有当对象中存储的数据为整型(int)的时候才能工作,其他时候,我
C++高级面试题:什么是模板特化(Template Specialization)?
模板特化(Template Specialization)是指针对特定的模板参数提供特定版本的模板实现。通过模板特化,可以为特定类型或值提供定制化的模板实现,以满足特定需求或提高性能。模板特化分为两种:类模板特化和函数模板特化。类模板特化:类模板特化是针对特定类型的模板参数提供定制的类模板实现。它允许在某些情况下,使用与通用模板不同的实现方式。
C++模板特化与偏特化 template <>的含义
C++模板特化与偏特化
C++模板的特化(specialization)和偏特化(partial specialization)
C++模板的特化及偏特化类模板全特化:对类中的某个成员函数进行特化处理类模板的偏特化范围偏特化函数模板全特化:函数模板偏特化: 模板函数和模板类有的时候可能需要对传入的不同类型进行不同的处理,比如说有的模板传入int或double类型都可以处理,但是传入char型则会出错,这时就需要模板特化的方式。 类模板全特化: 全特化即将模板类型里的所有类型参数全部具体指明之后处理,如下 template<typena
Template Specialization and Partial Template Specialization
<br />(1) 类模板特化<br />有时为了需要,针对特定的类型,需要对模板进行特化,也就是特殊处理.例如,stack类模板针对bool类型,因为实际上bool类型只需要一个二进制位,就可以对其进行存储,使用一个字或者一个字节都是浪费存储空间的.<br />template <class T><br />class stack {};<br />template < ><br />class stack<bool> { //…// };<br />上述定义中template < >告诉编
template specialization
template specializationtemplate is the fundation of generic programming in c++.template 分为 template function and template class.template用来解决一类通用问题,但是通用中总有意外。这时候,template specialization是对template的强有力的补充。最简单的一个例子:我想有一个compare,输入可以是任何built in 类型,输出为二者的大小关系。对于
template specialization on template class
c++中的模板在2个地方用到,一个是模板函数,另一个是模板类。模板类的使用,是为了更好地进行代码的重用,符合面向对象的设计。在很多说法中,模板类的使用是c++实现静态多态的一个重要手段,另一个手段则是函数重载,这里就不多说。在stl中,模板类被充分使用到了所有的容器中,用来实现容器的与实际所装内容无关的特点。一个典型的模板类如下所示:template class Compare { public: static bool IsEqual(const T& lh, const
C++ Class Template Specialization
内容:说明:类模板的使用示例代码:// Class_Template.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <string> #include <iostream> using namespace std; enum class Color { red, green, orange }; enu...
Partial Specialization of function template
1. function specialization vs overload 考虑一下,template specialization 归结为为给定类型选择正确的实现。并且解析发生在编译时。 现在将其与函数重载进行比较:它包括为给定类型的参数选择正确的函数。并且解析也发生在编译时。 从这个角度来看,这两个功能看起来非常相似。因此,通过函数重载可以达到与函数模板(部分或全部)专业化等效的功能,这是正常的。让我们用一个例子来说明。 考虑以下模板函数f: template <typename T> v
[C++]STL源码剖析:template specialization requires ‘template<>‘报错
报错: template specialization requires ‘template<>’ 为了使编译器将该语句认定为template specialization,我们需要在前面加上template<>关键字: 报错消失。 参考: https://stackoverflow.com/questions/15723850/why-does-this-code-give-the-error-template-specialization-requires-templa
c++ - template specialization and partial specialization
in this post, we are going to examining partialization. There are two kind of partialization, one is full specialization, and the other is partial specialization. full specialization partial...
C++ Template Specialization (模板特化)
个人理解这个东西说白了就是当模板类(或函数)的类型参数为某特定值时用对应的特化定义代之。 看个例子吧 #include using namespace std; template struct is_void { static const bool value = false; }; /* 上面的代码定义了一个简单的模板结构is_void的主版本,无论类型参
c++ - template function specialization
In this post, we are going to discuss the template function specialiation ; so what is "template function specialiation"? given an example, for the following function template template ...
linux template error: explicit specialization in non-namespace scope
将Windows上的项目移植到linux上,发现C++ template的实现还是有些不同,总的来说,linux上的template实现和C++标准更一致,而Windows上的实现更宽松。有些细微的不同在移植时还会有不小的麻烦,下面就是一例:namespace TestTemp{class CBase{public: template R UICall(const char *name, c
模板特化和偏模板特化例子(template specialization and partial template specialization)
测试环境: win7 64 g++ 4.8.1
模板特化(template specialization)
模板的特化(template specialization)分为两类:函数模板的特化和类模板的特化。(1)函数模板的特化:当函数模板需要对某些类型进行特别处理,称为函数模板的特化。例如: template 1 bool IsEqual(T t1, T t2)2 {3 return t1 == t2;4 }56
7. Template Specialization and Instantiation
Template specialization and instantiation
Template Explicit Specialization (模板显式特化)
模板显式特化(Template Explicit Specialization)是为特定的数据类型提供自定义的实现,而不是使用通用模板版本。这样做的目的是在某些情况下优化性能或提供特定的行为。模板显式特化是C++模板编程中强大的工具,它允许开发者为特定类型提供优化和定制的实现,从而提高性能和灵活性。在使用时应注意保持代码的一致性和可维护性,并通过充分的测试确保特化版本的正确性。联系我。

649




被折叠的 条评论
为什么被折叠?



