Oracle Solaris Studio 12.2:C++ 用户指南

A.2.92.1 值

opt 必须是下列值之一。

表 A–20 -template

值 

含义  

[no%]extdef

[不] 在独立的源文件中搜索模板定义。编译器使用 no%extdef 预定义 _TEMPLATE_NO_EXTDEF

[no%]geninlinefuncs

[不] 生成未引用的内联成员函数用于显式实例化的类模板。 

[no%]wholeclass

[不] 实例化整个模板类,而不仅仅是使用的这些函数。必须至少引用类的一个成员,否则编译器不实例化该类的任何成员。 

缺省值

如果未指定 -template 选项,则假定 -template=no%wholeclass,extdef

示例

请考虑以下代码:


example% cat Example.cc
     template <class T> struct S {
               void imf() {}
               static void smf() {}
     };

     template class S <int>;

     int main() {
     }
example%

指定了 -template=geninlinefuncs 时,即使在程序中没有调用 S 的两个成员函数,也会在目标文件中生成它们。


example% CC -c -template=geninlinefuncs Example.cc
example% nm -C Example.o

Example.o:

[Index] Value Size Type  Bind  Other Shndx Name
[5]       0   0    NOTY  GLOB  0     ABS   __fsr_init_value
[1]       0   0    FILE  LOCL  0     ABS   b.c
[4]      16   32   FUNC  GLOB  0     2     main
[3]     104   24   FUNC  LOCL  0     2     void S<int>::imf()
                                           [__1cBS4Ci_Dimf6M_v_]
[2]     64    20   FUNC  LOCL  0     2     void S<int>::smf()
                                           [__1cBS4Ci_Dsmf6F_v_]

另请参见

7.2.2 整个类实例化7.5 模板定义搜索