How to partially specialize a class but NOT specialize a member function:

Discussion in 'C++' started by mrstephengross, Aug 2, 2005.

  1. Ok, I've got a class with two template parameters (A and B), and a
    member function foo(). I want to specialize foo for a particular class
    A. Is this possible? The following code shows an example:

    =====================================
    template<typename A, typename B>
    class Foo
    {
    int foo() { return 5; }
    };


    template<typename B>
    int Foo<int, B>::foo() { return 6; }

    int main()
    {
    return 0;
    }
    ======================================

    The above example doesn't compile; g++ reports the following error:

    test.cpp:11: error: no `int Foo<int, B>::foo()' member function
    declared in class `Foo<int, B>'
    test.cpp:11: error: template definition of non-template `int Foo<int,
    B>::foo()'

    I take it this means I can't write function implementations for class
    specializations if the class specialization hasn't been declared...?

    Thanks,
    --Steve
     
    mrstephengross, Aug 2, 2005
    #1
    1. Advertising

  2. Re: How to partially specialize a class but NOT specialize a member

    mrstephengross wrote:
    > Ok, I've got a class with two template parameters (A and B), and a
    > member function foo(). I want to specialize foo for a particular class
    > A. Is this possible?


    Yes, as soon as you specialise your class template for that particular
    class A.

    > The following code shows an example:
    >
    > =====================================
    > template<typename A, typename B>
    > class Foo
    > {
    > int foo() { return 5; }
    > };
    >
    >
    > template<typename B>
    > int Foo<int, B>::foo() { return 6; }
    >
    > int main()
    > {
    > return 0;
    > }
    > ======================================
    >
    > The above example doesn't compile; g++ reports the following error:
    >
    > test.cpp:11: error: no `int Foo<int, B>::foo()' member function
    > declared in class `Foo<int, B>'
    > test.cpp:11: error: template definition of non-template `int Foo<int,
    > B>::foo()'
    >
    > I take it this means I can't write function implementations for class
    > specializations if the class specialization hasn't been declared...?


    Yes. To define a member you need to define the class first, of which the
    function is a member.

    V
     
    Victor Bazarov, Aug 2, 2005
    #2
    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:
    404
    Victor Bazarov
    Apr 8, 2005
  2. toton
    Replies:
    3
    Views:
    393
    Victor Bazarov
    Jan 25, 2007
  3. mathieu
    Replies:
    5
    Views:
    396
    Greg Herlihy
    May 15, 2008
  4. Replies:
    5
    Views:
    615
  5. Pavel
    Replies:
    2
    Views:
    254
    Pavel
    Aug 22, 2010
Loading...

Share This Page