Oracle® Solaris Studio 12.4:C++ 用户指南

退出打印视图

更新时间: 2014 年 12 月
 
 

6.6.2 模板专门化定义

必须定义声明的所有模板专门化。下例定义了上一节中声明的函数。

template <> unsigned twice<unsigned>(unsigned original)
    {return original << 1;}
#include <string.h>
template <> void sort<char*>(Array<char*> store)
    {int num_elems = store.GetSize();
      for (int i = 0; i < num_elems-1; i++)
          for (int j = i+1; j < num_elems; j++)
              if (strcmp(store[j-1], store[j]) > 0)
                  {char *temp = store[j];
                    store[j] = store[j-1];
                    store[j-1] = temp;}}