Template Specialization

2023-05-16

See this in the MSDN LibrarySee This in MSDN Library

Page Options

Conformance to ISO Standards for C++

Kate Gregory
Gregory Consulting

April 2004

Applies to:
Microsoft® Visual C++® .NET 2003
Microsoft® Visual C++® Toolkit 2003
Microsoft® 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 Specialization
The sample
Compiling and running
Conclusion
Related 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 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 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:


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 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:


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 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 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".

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

?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Template Specialization 的相关文章

随机推荐

  • PWM变模拟信号(积分电路 )

    就是简单的积分电路 频率不变 xff0c 积分后的电平相当于把高电平的电压和对应的时间的面积 xff0c 平均到一个周期里 基本上占空比是50 xff0c 转换的电压 xff0c 就是最高电压的50 xff0c 占空比30 xff0c 模拟
  • 如何生成汇编代码文件

    61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61
  • Ardupilot SITL——arducopter 操作步骤

    打开cygwin输入 cd ardupilot ArduCopter Tools autotest sim vehicle py map console xff08 默认master下版本arducopter xff0c 默认模拟 四轴 x
  • 3-catkin包介绍与构建

    本教程简单介绍ROS1的catkin包 至于为什么选择deepin而不是ROS通用的ubuntu 也仅仅是为了支持国产系统 鉴于本人水平有限 xff0c 如哪位攻城狮网友发现本文存在的问题 xff0c 烦请留言指正 xff0c 谢谢 cat
  • make与cmake入门

    文章目录 1 手动链接与编译2 make编译工具2 1 介绍makefile2 2 makefile三要素2 3 make工作原理2 4 实战案例1案例2案例3案例4 2 5 常见的自动化变量解析 3 使用cmake进行编译3 1 介绍cm
  • RK1126平台项目总结

    项目的大概流程是 从摄像头读数据然后经过ai分析 输出ai分析结果编码成2路venc的流 然后 起一个rtsp服务器 供后续模块 处理 但是从需求来看的话其实很简单 但是实际上做的时候还是会遇到一些坑的 程序起来之后会起一个http服务 等
  • Keil uVision5开发步骤备忘

    1 安装 2 授权 xff1a File gt License Management xff0c AddLIC 3 参照开发手册 xff0c 配置keil环境 xff1a 1 添加ET199模拟器 xff0c 复制SDK里的ET199Sim
  • 使用杉川3i-T1单线激光雷达和Cartographer库SLAM问题及解决

    用Cartographer做二维的激光SLAM xff0c 用杉川给的ROS例子发布LaserScan数据 xff0c 发现在Rviz中显示的数据 xff0c 本来应该是平直的墙变成弧形的 xff0c 建图也是混乱的 xff0c 如下图 x
  • TI毫米波雷达 MIMO (2TX4RX)设置

    我们知道xWR1243和xWR1443 EVM是3TX4RX雷达 xff0c xWR1642 EVM是2TX4RX雷达 xff0c 我们不仅要掌握1TX4RX模式的使用还要学会使用MIMO雷达模式 本文主要介绍如何在mmWave Studi
  • 以Apollo为例学习/分析自动驾驶运动规划算法

    这篇文章写得很粗糙 xff0c 作为我入门学习的笔记 xff0c 其中的思路 分析很可能不正确 xff0c 也希望有在工业界工作的朋友能给我提出一些意见建议 这将是一篇大杂烩 xff0c 也是我一直在学习的主线 想要一下子整理清楚还是很困难
  • 二次规划(QP)与OSQP求解器

    目录 二次规划 xff08 QP xff09 OSQP 求解器 OSQP eigen接口 二次规划 xff08 QP xff09 优化在很多领域都发挥着重要应用 xff0c 其中自动驾驶的运动规划可以看做一个优化问题 xff0c 根据实际情
  • Jetson TX2 入门 ——介绍

    暑假留校 xff0c 老师给我们拿了两块开发板 xff0c 一个是英伟达的Jetson TX2 xff0c 一个是up squared xff0c 让我们先熟悉开发板 xff0c 为明年的比赛做准备 这两个板子是前几届学长做比赛用过的 自己
  • 2.1.2 激光雷达

    更多内容 xff0c 请关注 xff1a github xff1a Autopilot Updating Notes gitee Autopilot Updating Notes 激光雷达是自动驾驶领域非常依赖的传感器 xff0c 越来越多
  • 2.1.5 GPS定位导航

    更多内容 xff0c 请关注 xff1a github xff1a Autopilot Updating Notes gitee Autopilot Updating Notes GPS是Global Positioning System
  • ROS2 + Qt5 cmake的CMakeLists.txt文件配置

    ROS2 QT实现学习笔记 1 1 功能包的创建和编译 ROS2 Foxy 43 Qt5 on Linux Platform 按上面两个文章配置后的目录结构 build CMakeLists txt include mainwindow h
  • 计算思维:二进制串的校验

    题目 xff1a 待传输的二进制串为 1001111 xff0c 若采用偶校验 xff0c 需增加几位校验位才能判断出哪一位传输错误 xff0c 若传输过去变为 1011111 xff0c 则如何判断出是哪一位出错 xff1f 请描述判断过
  • RK1126实现画中画功能 picture in picture for RK 1126

    项目中需要将两个摄像头的流 合并成一个流 主摄像头显示大画面 副摄像头 显示在右上角 实现类似画中画的功能 摸索了下 需要将 从摄像头中取到的 yuv数据进行处理即可 rga模块提供了关于图像处理的一些接口 使用improcess实现图像的
  • QT之基于图形框架QGraphicsView实现链路图

    1 前言 最近因项目需求 xff0c 需要制作一个可以绘制树结构的 事件链 插件 xff0c 于是呼找到了QT自带的一个画流程图的例子 diagramscene xff0c 还在网上找到了另外一个例子 然后我结合了两个demo实现了我的 事
  • 毕业生如何写简历的内容

    毕业生如何写简历的内容 2003年10月14日 14 47 一份能吸引读者注意力的简历 xff0c 能创造面试的机会及增加录取的机率 xff0c 所以必须使它兼备简洁 有序 有个性且不失重点等特色 xff0c 万万不可繁琐冗杂 简历并没有固
  • Template Specialization

    See This in MSDN Library Page Options Conformance to ISO Standards for C 43 43 Kate Gregory Gregory Consulting April 200