Topic
  • 3 replies
  • Latest Post - ‏2013-10-16T23:51:07Z by AE91_SHINJI_KANAI
U914_Fabrizio_De_Felice
4 Posts

Pinned topic Template classes' member function specialization

‏2013-10-15T14:08:15Z |

Can Rhapsody in C++ handle template member functions specialization?

Say I have a 

template <typename GenT> class MyTemplate 

{

  ....

void foo();

};

 

and i want to have one of more specializations (in the template specification)

 

template <>  void MyTemplate<AType>::foo() {

 ....

}

 

I haven't find a way to do it in the model. The only workaround is adding a specification epilog to the template class with the specialized functions definitions, but I don't like it as I can't really see them in the model like that.

  • AE91_SHINJI_KANAI
    AE91_SHINJI_KANAI
    146 Posts
    ACCEPTED ANSWER

    Re: Template classes' member function specialization

    ‏2013-10-16T23:51:07Z  

    Hello Shinji and many thanks for your reply. 

    Although your solution generates the exact code I need, unfortunately I can't use it as it would add a class in the model (the specialized template) that doesn't actually exist in the generated code (as it's commented out), and for documentation and process objectives I need to have a coherent model-code association.

    I think I'll try a different solution, maybe using the whole specialized classes (and not just the function) may be worth considering. 

    Thanks again

    Fabrizio

     

     

    Hello Fabrizio,

    Yes, I agree. It is always good to keep coherence between code and model. If you are going to use whole specialized classes, I suggest to consider following steps.
     
    6. Select "inline". (Do not turn "Template" check box). 
    7. Go to Properties tab.
    8. Choose "in_declaration"in CPP_CG::Operation::Inline.
    9. You will get the code like below:
     
    //## class MyTemplate<int>
    template <> class MyTemplate<int> {
        ////    Operations    ////
        
    public :
     
        //## operation foo()
        inline void foo() {
            //#[ operation foo()
            //#]
        }
    };
     
    Rhapsody support only inline operations of template specification (either in header or in declaration). As far as I know, to avoid parsing error I mentioned earlier, you have to choose "in_declaration". By the way, Rhapsody does not support RT/RE of implementation operations of template specification, so the code you entered in foo's operation body won't be round tripped. 
     
    There are several RFEs are submitted related to template usage in Rhapsody (click here). Please vote if you find any of them align with your business needs, thank you.

    --Shinji
  • AE91_SHINJI_KANAI
    AE91_SHINJI_KANAI
    146 Posts

    Re: Template classes' member function specialization

    ‏2013-10-16T05:59:57Z  

    Hello U914,

    Does the following steps work to satisfy your requirement?
     

    1. Create MyTemplate template class.
    2. Add foo() operation to the template class. 
    3. Create a specialized template class (e.g. <int>MyTemplate<>) by following Technote 1323750.
    4. Add foo() operation to <int>MyTemplate<> class as well. 
    5. Open Feature Window for the foo() added in previous step.
    6. Select "Inline" and "Template" check box. 
     
    At this stage, you will see the code similar to below at the very end of MyTemplate.h.
     
        //## class MyTemplate<int>
        template <> class MyTemplate<int> {
            ////    Operations    ////
            
        public :
        
            //## operation foo()
            template <> inline void foo();
        };
        
        template <> inline void MyTemplate<int>::foo() {
            //#[ operation foo()
            //#]
        }
     
    7. To avoid compilation error, comment out the entire block of MyTemplate<int> class and leaves only foo() template operation in the code. To achieve that, simply open Feature Window for MyTemplate<int> clas and set following properties. 
     
    CPP_CG::Class::SpecificationEpilog = */
    CPP_CG::Class::SpecificationProlog = /*
     
    8. You will see code generated as in attached image. 

    By the way, I see two limitations in this approach...
    1. Unable to remove "inline" keyword.
    2. RoundTrip doesn't work since the generated code is not understandable by Rhapsody's parser. 

    I hope it can be of any help. 

    Best Regard,

    --Shinji

     
  • U914_Fabrizio_De_Felice
    4 Posts

    Re: Template classes' member function specialization

    ‏2013-10-16T08:16:39Z  

    Hello U914,

    Does the following steps work to satisfy your requirement?
     

    1. Create MyTemplate template class.
    2. Add foo() operation to the template class. 
    3. Create a specialized template class (e.g. <int>MyTemplate<>) by following Technote 1323750.
    4. Add foo() operation to <int>MyTemplate<> class as well. 
    5. Open Feature Window for the foo() added in previous step.
    6. Select "Inline" and "Template" check box. 
     
    At this stage, you will see the code similar to below at the very end of MyTemplate.h.
     
        //## class MyTemplate<int>
        template <> class MyTemplate<int> {
            ////    Operations    ////
            
        public :
        
            //## operation foo()
            template <> inline void foo();
        };
        
        template <> inline void MyTemplate<int>::foo() {
            //#[ operation foo()
            //#]
        }
     
    7. To avoid compilation error, comment out the entire block of MyTemplate<int> class and leaves only foo() template operation in the code. To achieve that, simply open Feature Window for MyTemplate<int> clas and set following properties. 
     
    CPP_CG::Class::SpecificationEpilog = */
    CPP_CG::Class::SpecificationProlog = /*
     
    8. You will see code generated as in attached image. 

    By the way, I see two limitations in this approach...
    1. Unable to remove "inline" keyword.
    2. RoundTrip doesn't work since the generated code is not understandable by Rhapsody's parser. 

    I hope it can be of any help. 

    Best Regard,

    --Shinji

     

    Hello Shinji and many thanks for your reply. 

    Although your solution generates the exact code I need, unfortunately I can't use it as it would add a class in the model (the specialized template) that doesn't actually exist in the generated code (as it's commented out), and for documentation and process objectives I need to have a coherent model-code association.

    I think I'll try a different solution, maybe using the whole specialized classes (and not just the function) may be worth considering. 

    Thanks again

    Fabrizio

     

     

  • AE91_SHINJI_KANAI
    AE91_SHINJI_KANAI
    146 Posts

    Re: Template classes' member function specialization

    ‏2013-10-16T23:51:07Z  

    Hello Shinji and many thanks for your reply. 

    Although your solution generates the exact code I need, unfortunately I can't use it as it would add a class in the model (the specialized template) that doesn't actually exist in the generated code (as it's commented out), and for documentation and process objectives I need to have a coherent model-code association.

    I think I'll try a different solution, maybe using the whole specialized classes (and not just the function) may be worth considering. 

    Thanks again

    Fabrizio

     

     

    Hello Fabrizio,

    Yes, I agree. It is always good to keep coherence between code and model. If you are going to use whole specialized classes, I suggest to consider following steps.
     
    6. Select "inline". (Do not turn "Template" check box). 
    7. Go to Properties tab.
    8. Choose "in_declaration"in CPP_CG::Operation::Inline.
    9. You will get the code like below:
     
    //## class MyTemplate<int>
    template <> class MyTemplate<int> {
        ////    Operations    ////
        
    public :
     
        //## operation foo()
        inline void foo() {
            //#[ operation foo()
            //#]
        }
    };
     
    Rhapsody support only inline operations of template specification (either in header or in declaration). As far as I know, to avoid parsing error I mentioned earlier, you have to choose "in_declaration". By the way, Rhapsody does not support RT/RE of implementation operations of template specification, so the code you entered in foo's operation body won't be round tripped. 
     
    There are several RFEs are submitted related to template usage in Rhapsody (click here). Please vote if you find any of them align with your business needs, thank you.

    --Shinji