Template class specialization mapping for C++ (C++ code)
Two examples are provided, which demonstrate how Template Class specializations are generated. For procedural information about setting up Templates Class Specializations, see
Cpprev Setting up Template Classes and Template Class Specializations for generating Cpp code.
Example with Template Class present in the Model
The following example demonstrates how to set up template class and template class specializations in the Model.
Where:
• foo is a Template Class that has its Formal Template Parameters property set to typename T.
• foo<float> is a Template Class that has its Formal Template Parameters property blank.
• The Dependency(of Type Abstraction) must be stereotyped by the «C++ Dependency» stereotype with the tagged value of the C++ Specialization Parameters tag definition specifying the specialization parameters (float). In addition, the tagged value of the C++ In Implementation tag definition must be set to False to generate the #include in the header file.
If you generate foo, the header file contains the following text:
template<typename T>
class foo
{};
If you generate foo<float>, the header file contains the following text:
#include "foo.h"
template<>
class foo<float>
{};
Note that the name of the foo<float> class is irrelevant. When generating the foo<float> class, 'foo' is generated from the name of the associated Template Class and 'float' is generated from the tagged value of the C++ Specialization Parameters tag definition on the Dependency.
Example with Template Class not present in the Model
The following example demonstrates how to set up template class specializations in a Model when the associated template class is not present in the Model.
foo<float> i s a template class specialization of a class named foo, which is not present in the Model:
• foo<float> is a Template Class that has its Formal Template Parameters property blank.
• foo<float> is stereotyped by the C++ Class stereotype.
• The specialization parameters (float) are specified through the tagged value of the C++ Specialization Parameters tag definition.
• The #include for the template class is generated through the C++ Header Include tag definition.
If you generate foo<float>, the header file contains the following text:
#include "foo.h"
template<>
class foo<float>
{};
When generating the foo<float> class, 'foo' is generated from the Class name up to the opening angle bracket and 'float' is generated from the tagged value of the C++ Specialization Parameters tag definition.