question about Partial Template Specialization

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

    #1

    question about Partial Template Specialization

    I have some code like following:
    template<class T>
    class A
    {
    public:
    A(T& t) : m_b(t){};
    void Func()
    {
    m_b.DoSome();
    };
    private:
    B<tm_b;
    };
    ---------------------------------------
    template<class T>
    class B
    {
    public:
    void DoSome() {
    cout << "type t";
    };
    private:
    T &t;
    };
    template<>
    void B<int>::DoSome( )
    {
    cout << "type int";
    }
    ---------------------------------------
    main()
    {
    A<inta;
    a.Func();
    }

    the output is "type t" instead of "type int", why?! any help is
    appericiated.

  • Gianni Mariani

    #2
    Re: question about Partial Template Specialization

    Leon Wu wrote:
    I have some code like following:
    template<class T>
    class A
    {
    public:
    A(T& t) : m_b(t){};
    void Func()
    {
    m_b.DoSome();
    };
    private:
    B<tm_b;
    };
    ---------------------------------------
    template<class T>
    class B
    {
    public:
    void DoSome() {
    cout << "type t";
    };
    private:
    T &t;
    };
    template<>
    void B<int>::DoSome( )
    {
    cout << "type int";
    }
    ---------------------------------------
    main()
    {
    A<inta;
    a.Func();
    }
    >
    the output is "type t" instead of "type int", why?! any help is
    appericiated.
    >
    Please post a complete compilable program...

    After fixing a number of compile errors you get this -
    this says "type int"

    #include <iostream>

    template<class T>
    class B;

    template<class T>
    class A
    {
    public:
    A() {};
    A(T& t) : m_b(t){};
    void Func()
    {
    m_b.DoSome();
    };
    private:
    B<Tm_b;
    };
    //---------------------------------------
    template<class T>
    class B
    {
    public:
    void DoSome() {
    std::cout << "type t";
    };
    private:
    T &t;
    };
    template<>
    void B<int>::DoSome( )
    {
    std::cout << "type int";
    }
    //---------------------------------------
    int main()
    {
    A<inta;
    a.Func();
    }

    Comment

    • Leon Wu

      #3
      Re: question about Partial Template Specialization

      I write this code snipet according to my program, after test it, I find
      it work well too.
      after some investigation, I found the different between my program and
      this code snipet is
      all class is inside different head file. just as follows:
      main.cpp-------------------------------------
      #include <iostream>
      #include "A.h"
      int main()
      {
      int c = 10;
      A<inta(c);
      a.Func();
      return 0;
      }
      A.h---------------------------
      #ifndef __RCBB_A_H__
      #define __RCBB_A_H__
      #include "b.h"
      template<class T>
      class A
      {
      public:
      A(T& t) : m_b(t){};
      void Func()
      {
      m_b.DoSome();
      };
      private:
      B<Tm_b;
      };

      B.h----------------------------
      #ifndef __RCBB_B_H__
      #define __RCBB_B_H__
      template<class T>
      class B
      {
      public:
      B(T& t) : m_t(t){};
      void DoSome()
      {
      cout << "type t";
      };
      private:
      T &m_t;
      };
      SpecB.h--------------------------
      #ifndef __RCBB_SPECB_H_ _
      #define __RCBB_SPECB_H_ _
      #include <iostream>
      #include "B.h"
      template<>
      void B<int>::DoSome( )
      {
      std::cout << "type int";
      }
      ----------------------------------------------------------------------------
      If the code is like above, the output will be "type t".

      "Gianni Mariani дµÀ£º
      "
      Leon Wu wrote:
      I have some code like following:
      template<class T>
      class A
      {
      public:
      A(T& t) : m_b(t){};
      void Func()
      {
      m_b.DoSome();
      };
      private:
      B<tm_b;
      };
      ---------------------------------------
      template<class T>
      class B
      {
      public:
      void DoSome() {
      cout << "type t";
      };
      private:
      T &t;
      };
      template<>
      void B<int>::DoSome( )
      {
      cout << "type int";
      }
      ---------------------------------------
      main()
      {
      A<inta;
      a.Func();
      }

      the output is "type t" instead of "type int", why?! any help is
      appericiated.
      >
      Please post a complete compilable program...
      >
      After fixing a number of compile errors you get this -
      this says "type int"
      >
      #include <iostream>
      >
      template<class T>
      class B;
      >
      template<class T>
      class A
      {
      public:
      A() {};
      A(T& t) : m_b(t){};
      void Func()
      {
      m_b.DoSome();
      };
      private:
      B<Tm_b;
      };
      //---------------------------------------
      template<class T>
      class B
      {
      public:
      void DoSome() {
      std::cout << "type t";
      };
      private:
      T &t;
      };
      template<>
      void B<int>::DoSome( )
      {
      std::cout << "type int";
      }
      //---------------------------------------
      int main()
      {
      A<inta;
      a.Func();
      }

      Comment

      • Ivan Novick

        #4
        Re: question about Partial Template Specialization

        The reason you are not seeing the specialization work as you expect
        appears to be that you have not included in the header file that
        contains your specialization. That header must be included too so the
        compiler will know there is a specialization.

        Comment

        • Leon Wu

          #5
          Re: question about Partial Template Specialization

          Thank you. I solve this problem, now I put the declaration of
          specialization in B.h, and move the definition into a cpp, it work
          well.
          "Ivan Novick дµÀ£º
          "
          The reason you are not seeing the specialization work as you expect
          appears to be that you have not included in the header file that
          contains your specialization. That header must be included too so the
          compiler will know there is a specialization.

          Comment

          Working...