casting template using const_cast

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • puru

    casting template using const_cast

    I have a template class like below.
    template <TInt Sclass XYZ
    {

    }

    const XYZ<10abc; //Instance of the class

    How do remove the constantness of abc using const_cast. I mean, I want
    the syntax for that. Something like const_cast<XYZ< 10>abc gives
    compiler error.
  • Barry

    #2
    Re: casting template using const_cast

    On 11ÔÂ7ÈÕ, ÏÂÎç10ʱ01·Ö, puru <purushotta...@ gmail..comwrote :
    I have a template class like below.
    template <TInt Sclass XYZ
    {
    >
    }
    >
    const XYZ<10abc; //Instance of the class
    >
    How do remove the constantness of abc using const_cast. I mean, I want
    the syntax for that. Something like const_cast<XYZ< 10>abc gives
    compiler error.
    const_cast can only do with pointer, reference and pointer to
    member(function ).

    try this

    XYZ<10>* p = const_cast<XYZ< 10>*>(&abc);

    --
    Best Regards
    Barry

    Comment

    • peter koch

      #3
      Re: casting template using const_cast

      On 7 Nov., 15:01, puru <purushotta...@ gmail.comwrote:
      I have a template class like below.
      template <TInt Sclass XYZ
      {
      >
      }
      >
      const XYZ<10abc;  //Instance of the class
      >
      How do remove the constantness of abc using const_cast. I mean, I want
      the syntax for that. Something like const_cast<XYZ< 10>abc gives
      compiler error.
      Others have explained how, but I believe having to cast is a design
      error. Instead of casting yourself out of your errors, you should
      correct the design, which in the long run is going to save you time
      and trouble.

      /Peter

      Comment

      Working...