C++ Coding Tips - Chapter3. Templates

原创 2002年04月26日 09:32:00
C++ Coding Tips
Betta Jin <betajin@hotmail.com>
[Draft] April {16,19,25}, 2002

Chapter3. Templates
Section1. Compile time routing
  • Compile time if-else structure:
    
     < _is_array>
     deallocator ;
    
     <>
     deallocator<>
    {
    :
         < T>
         free_ptr(T *  & p)
        {
            ASSERT(p != 0) ;
            [] p ;
        }
    } ;
     <>
     deallocator<>
    {
    :
         < T>
         free_ptr(T *  & p)
        {
            ASSERT(p != 0) ;
             p ;
        }
    } ;
    
     < T,  _is_array=>
     CHello
    {
        ...
        T * m_p ;
         free()
        {
            deallocator<_is_array&gt::free_ptr(m_p) ;
        }
        ...
    } ;
    
    Note: This sample demonstrates a way to wrap the delete/delete[] operation in a same function without an if-else routing. Here goes the if-else structure, and more, there will be switch-case sturcture in the same way. I think the '_is_array' parameter here can be called as 'value traits' or 'switch traits'.
  • Both VC6 and VC7 cannot compile the following code. But g++3 does! So, for compatible considerations, I do not suggest such code:
     < T,  _is_array>
     cls_any ;
     < T>
     cls_any<T, >
    {
        ...
    } ;
     < T>
     cls_any<T, >
    {
        ...
    } ;
    
    Note: This is something of partial specialization. The code in my way(1) for partial specializtion is nice enough for such requirements and can be accepted by both VC6/VC7/g++3. There are some tricks to implement compound class partial specialization. I'll describe it later.
  • VC6, VC7 and g++3 cannot compile such code at all:
     <class T,  _is_array>
     func_any(T *  & p) ;
     < T>
     func_any<T, >(T *  & p)
    {
        ...
    }
     T>
     func_any<T, >(T *  & p)
    {
        ...
    }
    
Section2. Any-type routing
  • Description:
    There are libraries and they are likely to provide some same functions. But the providers did not make any type of conference to specify the naming of functions/APIs. This is a problem. When we write a common lib, we are forced to declare that the library is for or based on some library. It's just a burden that if some one wants the library that is based on one specified library to be used in another environment, he must write a piece of customed code to adapt the library. If the coding task is done with little ugly code, it is just so lucky. But some time, he cannot use the library in that there is no base library for him to use. So no chance a adapter, he has to write his own code instead to use the library. This is not only a compatible problem, but the library's self-adaptability.
    I'll try to give a solution for such requirement by examples.
  • Any-type string:
  • Use case of Any-type string:

C++ Coding Tips - Chapter1

C++ Coding TipsBetta Jinbetajin@hotmail.com>April 15, 2002Chapter1. Coding StylesSection1. Let globa...
  • Betta
  • Betta
  • 2002年04月18日 09:28
  • 2217

终于把C++ Templates看完了~!

4月份的时候就从pangwa那里借来了英文版的书,此书恐怕是到现在看得最痛苦的一本,比大一那会儿通读的《Thinking in Java》E版还难受。尤其是第一篇讲Template技术细节的时候巨细靡...
  • saintony
  • saintony
  • 2007年09月15日 19:21
  • 935

C++可变长参数模版(Variadic Templates)

/* * File: main.cpp * Author: Vicky.H * Email: eclipser@163.com */ #include template struc...
  • eclipser1987
  • eclipser1987
  • 2013年01月11日 15:51
  • 2102

coding tips

1. Linux 动态连接库需要存取主程序里定义的全局变量,在链接主程序的时候,gcc 加上 -Wl,--export-dynamic 参数。 2. automake configer 自定...
  • HuangLiGuan
  • HuangLiGuan
  • 2018年01月04日 11:08
  • 9

Coding Tips

1. for循环是开界的。它的一般形式为: for(; 条件表达式>; ) 语句; 初始化总是一个赋值语句, 它用来给循环控制变量赋初值; 条件表达式是一个关系表达式, 它决定什么时候退出...
  • anlou
  • anlou
  • 2012年09月01日 15:21
  • 292

《C++ Templates 中文版》[陈伟柱译] 续2

 第3部分 模板与设计  第14章 模板的多态威力  14.1 动多态  14.2 静多态  14.3 动多态和静多态 14.3.1 术语 14.3.2 优点和缺点 14.3.3 将两种多态结合起来 ...
  • zhangkewei
  • zhangkewei
  • 2007年10月11日 12:08
  • 1153

【c++ templates读书笔记】【1】函数模板

1、定义函数模板: template inline T const& max (T const& a, T const& b){ return a < b ? b : a; } 解释:templ...
  • ruan875417
  • ruan875417
  • 2015年09月19日 14:08
  • 304

Coding templates and formatters

原文地址:https://developer.connectopensource.org/display/CONNECTWIKI/Coding+templates+and+formatters We ...
  • chendc201
  • chendc201
  • 2013年11月12日 17:45
  • 838

读书摘要:C++ Template:The Complete Guide

Chapter 1 About This Book1.4    对于模板编程中const的使用,推荐使用 int const 而不是 const int 的风格Chapter 2. Function ...
  • lovekatherine
  • lovekatherine
  • 2008年03月06日 06:46
  • 3641

c++Templates学习笔记(三)

2.第二部分:深入模版基础 (1)成员函数模版不能声明为虚函数,因为虚函数调用机制的普通实现都使用了一个大小固定的表,每个虚函数都对应表的一个人口,然而,成员函数模版的实例化个数,要等到整个程序都翻...
  • xd1103121507
  • xd1103121507
  • 2013年01月16日 11:07
  • 228
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:C++ Coding Tips - Chapter3. Templates
举报原因:
原因补充:

(最多只允许输入30个字)