IBM Z and LinuxONE - Languages - Group home

Recursively instantiated template specializations

  

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation. The definition created from a template instantiation is called a specialization. A large number of recursive template instantiations might cause an out-of-memory error because of the complexity and amount of code generated.


Starting from z/OS V1R13, the TEMPLATEDEPTH(number) compiler option can be used to specify the maximum number of recursively instantiated template specializations that are processed by the compiler. The number can be a value in the range of 1 and INT_MAX. If a program attempts to recursively instantiate more templates than the number specified, compilation halts and an error message is issued. If an invalid value is specified, the default value of 300 is used.


To allow the following code in myprogram.cpp to be compiled successfully:

template <int n> void foo() {

 foo<n-1>();

}

template <> void foo<0>() { }

  int main() {

  foo<400>();

}

Enter:

xlc++ myprogram.cpp -qtemplatedepth=400


Details about the TEMPLATEDEPTH compiler option can be found in z/OS XL C/C++ User’s Guide.