How To: Specialize template functions using IBM Rational Rhapsody

2 minute read

In this How To, I will guide you to create specialized template functions within IBM Rational Rhapsody.

If you follow the guide to specialize member functions within classes, you will get a code like this:

//## class MyConvert
class MyConvert {
////    Constructors and destructors    ////

public :



    ////    Operations    ////

    //## operation strtox(char*,char*)
    template <typename T> inline static T strtox(char* in, char** end);

    //## operation strtox(char*,char**)
    template <> inline double strtox<double>(char* in, char** end) {
        //#[ operation strtox(char*,char**)
        return strtod(in, end);

In other words, function will be defined in class deceleration. However this is not allowed in C++. Compiler will complain:

error: explicit specialization in non-namespace scope 'class MyConvert'

We need to declare and implement those template specializations outside of the class. One workaround to this problem is not to define your functions inside the class, but in the package itself. Therefore, there will be nothing to implement within a class.

Here are the steps for that:

  • Right click the Package and click Add New -> Function.

IBM Rational Rhapsody Architect for Software

  • Go into Features of the new function. Change its name, mark it as Template.

  • Go into Template Parameters and add a new class / typename.

  • Now, this is our base function. You may want to add arguments, change return type now.  If you are done, copy the function using right click or good old Control + C. Then paste it to Functions in the current package. Optional: If you won’t want a default function for all types which are not specialized, go into Properties. Change View Common to all. CG::Generate::Specification will only create the prototype for the function. Thus, any typename/class which is not specialized will fail to link.

  • Go into Features of the new function, namely strtox_copy . Click Template Parameters on the tab and point Primary Template to original function we created before. This will change function name, arguments, etc. Further, template types will be copied too.

  • Delete the Template which appeared on the upper area. This will allow us to specialize this typename.

  • Now we can choose a type for the deleted template.

  • Currently the function is not inlined and this will cause compilation error. Go to Properties tab, CPP_CG::Operation::Inline and choose in_header

When we generate the code now, it will appear like this in the header:

#ifndef TemplateTest_TemplateTest_H
#define TemplateTest_TemplateTest_H

template <typename T> void strtox();

template <> void strtox<double>();

template <typename T> void strtox() {

template <> void strtox<double>() {


And now it’s compilable.