About partially specify template member function

Discussion in 'C++' started by PengYu.UT@gmail.com, Apr 5, 2006.

  1. Guest

    I have the following two program. The first one compiles well, but the
    second one doesn't. The only difference between them is one more
    template parameter is added for the second program.

    Would you please help on how to make the second program work?

    Thanks,
    Peng

    /************first************************/
    #include <iostream>

    class tag1;
    class tag2;

    template <typename T>
    class A{
    public:
    void print();
    };

    template <>
    void A<tag1>::print() {
    std::cout << "tag1" << std::endl;
    }

    template <>
    void A<tag2>::print() {
    std::cout << "tag2" << std::endl;
    }

    int main(int ac, char* av[])
    {
    A<tag1> a1;
    a1.print();

    A<tag2> a2;
    a2.print();
    }
    /************end first************************/

    /**************second*************************/
    #include <iostream>

    class tag1;
    class tag2;

    template <typename T1, typename T>
    class A{
    public:
    void print();
    };

    template <typename T1>
    void A<T1, tag1>::print() {
    std::cout << "tag1" << std::endl;
    }

    template <typename T1>
    void A<T1, tag2>::print() {
    std::cout << "tag2" << std::endl;
    }

    int main(int ac, char* av[])
    {
    A<int, tag1> a1;
    a1.print();

    A<int, tag2> a2;
    a2.print();
    }
    /**************end second*************************/
     
    , Apr 5, 2006
    #1
    1. Advertising

  2. Rolf Magnus Guest

    wrote:

    > I have the following two program. The first one compiles well, but the
    > second one doesn't. The only difference between them is one more
    > template parameter is added for the second program.
    >
    > Would you please help on how to make the second program work?


    You need to specialize the whole class, not just the function. And your
    syntax for partial specialization is not correct. See below.

    > #include <iostream>
    >
    > class tag1;
    > class tag2;
    >
    > template <typename T1, typename T>
    > class A{
    > public:
    > void print();
    > };


    template <>
    template<typename T1>
    class A<T1, tag1> {
    public:
    void print();
    };

    template<>
    > template <typename T1>
    > void A<T1, tag1>::print() {
    > std::cout << "tag1" << std::endl;
    > }


    template <>
    template<typename T1>
    class A<T1, tag2> {
    public:
    void print();
    };

    template<>
    > template <typename T1>
    > void A<T1, tag2>::print() {
    > std::cout << "tag2" << std::endl;
    > }
    >
    > int main(int ac, char* av[])
    > {
    > A<int, tag1> a1;
    > a1.print();
    >
    > A<int, tag2> a2;
    > a2.print();
    > }
     
    Rolf Magnus, Apr 5, 2006
    #2
    1. Advertising

  3. Guest

    > > #include <iostream>
    > >
    > > class tag1;
    > > class tag2;
    > >
    > > template <typename T1, typename T>
    > > class A{
    > > public:
    > > void print();
    > > };

    >
    > template <>
    > template<typename T1>
    > class A<T1, tag1> {
    > public:
    > void print();
    > };
    >
    > template<>
    > > template <typename T1>
    > > void A<T1, tag1>::print() {
    > > std::cout << "tag1" << std::endl;
    > > }

    >
    > template <>
    > template<typename T1>
    > class A<T1, tag2> {
    > public:
    > void print();
    > };
    >
    > template<>
    > > template <typename T1>
    > > void A<T1, tag2>::print() {
    > > std::cout << "tag2" << std::endl;
    > > }
    > >
    > > int main(int ac, char* av[])
    > > {
    > > A<int, tag1> a1;
    > > a1.print();
    > >
    > > A<int, tag2> a2;
    > > a2.print();
    > > }


    The above program works. But I have to define
    class A<T1, tag1> and class A<T1, tag2> even if they exactly the same
    as class A<T1, T>. This might introduce a lot of redundances if the
    class A is a very big class and only the "print" functions are slightly
    different for different template instantiations.

    Is there any walkaround?
     
    , Apr 5, 2006
    #3
  4. Rolf Magnus Guest

    wrote:

    >> > #include <iostream>
    >> >
    >> > class tag1;
    >> > class tag2;
    >> >
    >> > template <typename T1, typename T>
    >> > class A{
    >> > public:
    >> > void print();
    >> > };

    >>
    >> template <>
    >> template<typename T1>
    >> class A<T1, tag1> {
    >> public:
    >> void print();
    >> };
    >>
    >> template<>
    >> > template <typename T1>
    >> > void A<T1, tag1>::print() {
    >> > std::cout << "tag1" << std::endl;
    >> > }

    >>
    >> template <>
    >> template<typename T1>
    >> class A<T1, tag2> {
    >> public:
    >> void print();
    >> };
    >>
    >> template<>
    >> > template <typename T1>
    >> > void A<T1, tag2>::print() {
    >> > std::cout << "tag2" << std::endl;
    >> > }
    >> >
    >> > int main(int ac, char* av[])
    >> > {
    >> > A<int, tag1> a1;
    >> > a1.print();
    >> >
    >> > A<int, tag2> a2;
    >> > a2.print();
    >> > }

    >
    > The above program works. But I have to define
    > class A<T1, tag1> and class A<T1, tag2> even if they exactly the same
    > as class A<T1, T>. This might introduce a lot of redundances if the
    > class A is a very big class and only the "print" functions are slightly
    > different for different template instantiations.
    >
    > Is there any walkaround?


    Put the actual code in a non-member function that is called by print. Then
    you can partially specialize that function.
     
    Rolf Magnus, Apr 5, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Old Wolf
    Replies:
    4
    Views:
    420
    Victor Bazarov
    Apr 8, 2005
  2. mrstephengross
    Replies:
    1
    Views:
    302
    Victor Bazarov
    Aug 2, 2005
  3. Rahul
    Replies:
    8
    Views:
    413
    Lionel B
    Jul 16, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    809
    Thomas J. Gritzan
    Oct 26, 2008
  5. Igor R.
    Replies:
    2
    Views:
    234
    Igor R.
    Jun 1, 2011
Loading...

Share This Page