class template - specialisation for a specific method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • (2b|!2b)==?

    class template - specialisation for a specific method

    I would like to know if I can specialize only a specific method for a
    class template.

    Is the (specialization ) code below valid?


    template <typename T1, typename T2>
    class MyClass
    {
    public:
    MyClass(const T1&);
    ~MyClass();

    //Not implemented inline
    MyClass * instance(const string& arg1);

    T1 foo1(T1){ //implementation here ; }
    T2 foo2(T2){ //implementation here ; }
    T1 foobar1(T1,T2){ //implementation here ; }
    T2 foobar2(T2,T1){ //implementation here ; }
    }

    //Specialization of only the instance() method
    template <>
    MyClass<int, string* MyClass<int, string>::instan ce(const string& arg1)
    {
    }

    template <>
    MyClass<double, double* MyClass<double, double>::instan ce(const
    string& arg1)
    {
    }
  • Victor Bazarov

    #2
    Re: class template - specialisation for a specific method

    (2b|!2b)==? wrote:
    I would like to know if I can specialize only a specific method for a
    class template.
    >
    Is the (specialization ) code below valid?
    >
    >
    template <typename T1, typename T2>
    class MyClass
    {
    public:
    MyClass(const T1&);
    ~MyClass();
    >
    //Not implemented inline
    MyClass * instance(const string& arg1);
    >
    T1 foo1(T1){ //implementation here ; }
    T2 foo2(T2){ //implementation here ; }
    T1 foobar1(T1,T2){ //implementation here ; }
    T2 foobar2(T2,T1){ //implementation here ; }
    }
    >
    //Specialization of only the instance() method
    template <>
    MyClass<int, string* MyClass<int, string>::instan ce(const string& arg1)
    {
    }
    >
    template <>
    MyClass<double, double* MyClass<double, double>::instan ce(const
    string& arg1)
    {
    }
    I don't believe you need the 'template<>'. Each function is not a
    specialisation (because the members are not templates), it's an
    implementation and for that you just indicate for which type you are
    implementing the member function.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • (2b|!2b)==?

      #3
      Re: class template - specialisation for a specific method

      Victor Bazarov wrote:
      (2b|!2b)==? wrote:
      >I would like to know if I can specialize only a specific method for a
      >class template.
      >>
      >Is the (specialization ) code below valid?
      >>
      >>
      >template <typename T1, typename T2>
      >class MyClass
      >{
      >public:
      > MyClass(const T1&);
      > ~MyClass();
      >>
      > //Not implemented inline
      > MyClass * instance(const string& arg1);
      >>
      > T1 foo1(T1){ //implementation here ; }
      > T2 foo2(T2){ //implementation here ; }
      > T1 foobar1(T1,T2){ //implementation here ; }
      > T2 foobar2(T2,T1){ //implementation here ; }
      >}
      >>
      >//Specialization of only the instance() method
      >template <>
      >MyClass<int, string* MyClass<int, string>::instan ce(const string& arg1)
      >{
      >}
      >>
      >template <>
      >MyClass<double , double* MyClass<double, double>::instan ce(const
      >string& arg1)
      >{
      >}
      >
      I don't believe you need the 'template<>'. Each function is not a
      specialisation (because the members are not templates), it's an
      implementation and for that you just indicate for which type you are
      implementing the member function.
      >
      V
      Thanks Victor,

      Indeed, it would appear that you are correct. Not adding the template<>
      also gets rid of my compilation errors. It seems a lot of the
      documentation out there on template specialization are using incorrect
      terminology + examples :/

      Comment

      • Andrey Tarasevich

        #4
        Re: class template - specialisation for a specific method

        (2b|!2b)==? wrote:
        I would like to know if I can specialize only a specific method for a
        class template.
        >
        Is the (specialization ) code below valid?
        ...
        //Specialization of only the instance() method
        template <>
        MyClass<int, string* MyClass<int, string>::instan ce(const string& arg1)
        {
        }
        >
        template <>
        MyClass<double, double* MyClass<double, double>::instan ce(const
        string& arg1)
        {
        }
        Yes, it is valid. And yes, you do need the 'template<>' bit when you
        perform explicit specialization. What you are trying to do is
        illustrated exactly by an example in 14.7/17 in the language standard.

        --
        Best regards,
        Andrey Tarasevich

        Comment

        • Andrey Tarasevich

          #5
          Re: class template - specialisation for a specific method

          Victor Bazarov wrote:
          (2b|!2b)==? wrote:
          >I would like to know if I can specialize only a specific method for a
          >class template.
          >>
          >Is the (specialization ) code below valid?
          >>
          >>
          >template <typename T1, typename T2>
          >class MyClass
          >{
          >public:
          > MyClass(const T1&);
          > ~MyClass();
          >>
          > //Not implemented inline
          > MyClass * instance(const string& arg1);
          >>
          > T1 foo1(T1){ //implementation here ; }
          > T2 foo2(T2){ //implementation here ; }
          > T1 foobar1(T1,T2){ //implementation here ; }
          > T2 foobar2(T2,T1){ //implementation here ; }
          >}
          >>
          >//Specialization of only the instance() method
          >template <>
          >MyClass<int, string* MyClass<int, string>::instan ce(const string& arg1)
          >{
          >}
          >>
          >template <>
          >MyClass<double , double* MyClass<double, double>::instan ce(const
          >string& arg1)
          >{
          >}
          >
          I don't believe you need the 'template<>'. Each function is not a
          specialisation (because the members are not templates), it's an
          implementation and for that you just indicate for which type you are
          implementing the member function.
          ...
          That would be the case if the OP made explicit specializations of the
          entire class template

          template <class MyClass<int, string{
          ...
          MyClass * instance(const string& arg1);
          ...
          }

          template <class MyClass<double, double{
          ...
          MyClass * instance(const string& arg1);
          ...
          }

          and then was providing the definitions for the method 'instance' in each
          specialization

          MyClass<int, string*
          MyClass<int, string>::instan ce(const string& arg1)
          {
          }

          MyClass<double, double*
          MyClass<double, double>::instan ce(const string& arg1)
          {
          }

          But thats' not what the OP is trying to do. If I understood it
          correctly, the intent is to provide explicit specializations for the
          method only, without introducing explicit specializations for the entire
          class template. In that case the 'template<>' portion is required. The
          OP's original code is correct.

          --
          Best regards,
          Andrey Tarasevich

          Comment

          • Andrey Tarasevich

            #6
            Re: class template - specialisation for a specific method

            (2b|!2b)==? wrote:
            >
            Indeed, it would appear that you are correct. Not adding the template<>
            also gets rid of my compilation errors. It seems a lot of the
            documentation out there on template specialization are using incorrect
            terminology + examples :/
            Are you sure you are posting the same code you are trying to compile?
            Because in the conforming compiler it should be the other way around:
            your original code should compile fine (aside from teh missing ';' and
            other unrelated problems), while removing the 'template<>' part from the
            method specialization should produce compiler errors.

            --
            Best regards,
            Andrey Tarasevich

            Comment

            Working...