Question & Answer
Question
How do you use IBM Rational Rhapsody to generate unnamed namespace in source file?
Cause
C++ allows you to declare an unnamed namespace as an alternative to global static variable.
You can use below properties in Rhapsody model to generate namespace in source file.
- CPP_CG::Package:DefineNameSpace
- CPP_CG::Package::NameSpaceName
But only enabling CPP_CG:Package::DefineNameSpace and leaving CPP_CG::Package::NameSpaceName empty won't generate unnamed namespace as expected.
Rhapsody would use the package name as the namespace by default if no value given in CPP_CG::Package::Name.
Answer
You have two solutions to generate unnamed namespace from Rhapsody model as follows;
Solution 1:
Directly put unnamed namespace code namespace {" and "} into below corresponding properties.
CPP_CG::Class::SpecificationProlog
CPP_CG::Class::SpecificationEpilog
CPP_CG::Class::ImplementationProlog
CPP_CG::Class::ImplementationEpilog
Note: You can also apply this solution to define a namespace on a specific class or even operation.
For example, if you hoping to create namespace against a class, then you should put the corresponding code into below properties of the target class:
CPP_CG::Class::SpecificationProlog
CPP_CG::Class::SpecificationEpilog
CPP_CG::Class::ImplementationProlog
CPP_CG::Class::ImplementationEpilog
Solution 2:
- Define an empty definition (ANONYMOUS_NAMESPACE)
- Put the definition name into property CPP_CG::Package::NameSpaceName
Below code is expected in the source file.
-------------------------start----------------------------------
#define ANONYMOUS_NAMESPACE
namespace ANONYMOUS_NAMESPACE{
}
--------------------------end-----------------------------------
Note: A corresponding product enhancement requesting Rhapsody Code Generation simply creating unnamed(anonymous) namespace (RFE ID: 37791) is now registered in IBM RFE Community Site.
Product Synonym
Rational Rhapsody
Was this topic helpful?
Document Information
Modified date:
27 May 2022
UID
swg21645128