Class member specialization

Discussion in 'C++' started by braton@gmail.com, Feb 26, 2008.

  1. Guest

    Hello

    I need some help with template functions specialization. Consider the
    following code:

    #include <iostream>

    template<class T, int I>
    class C {
    public:
    int Member(void);
    };

    template<class T, int I>
    int C<T,I>::Member(void) {
    T t();
    return I;
    }

    int main(void) {

    C<int,3> c;
    std::cout << c.Member() << std::endl;

    return 0;
    }

    So far, everything works fine. When I provide explicit specialization,
    everything is OK too:

    template<>
    int C<int,5>::Member(void) {
    return 555;
    }

    The problem appears, when I try to partially specialize that function:

    template<class U>
    int C<U,3>::Member(void) {
    U u();
    return 6;
    }

    I got some errors under Visual C++ Express:

    -) error C3860: template argument list following class template name
    must list parameters in the order used in template parameter list
    -) error C2995: 'int C<T,I>::Member(void)' : function template has
    already been defined
    -) error C2264: 'C<T,I>::Member' : error in function definition or
    declaration; function not called

    and under Comeau online compiler:

    -) "ComeauTest.c", line 21: error: template argument list must match
    the parameter list
    int C<U,3>::Member(void) {

    Is this possible to do in standard C++ way? Thanks for your help.

    braton
    , Feb 26, 2008
    #1
    1. Advertising

  2. wrote:
    > [..]
    > The problem appears, when I try to partially specialize that function:
    > [..]
    >
    > Is this possible to do in standard C++ way? Thanks for your help.


    No. C++ does not allow partial specialisations of template functions.
    Use overloading or wrap it into a class and partially-specialise that
    class.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Feb 26, 2008
    #2
    1. Advertising

  3. Ondra Holub Guest

    On 26 Ún, 15:21, wrote:
    > Hello
    >
    > I need some help with template functions specialization. Consider the
    > following code:
    >
    > #include <iostream>
    >
    > template<class T, int I>
    > class C {
    > public:
    > int Member(void);
    >
    > };
    >
    > template<class T, int I>
    > int C<T,I>::Member(void) {
    > T t();
    > return I;
    >
    > }
    >
    > int main(void) {
    >
    > C<int,3> c;
    > std::cout << c.Member() << std::endl;
    >
    > return 0;
    >
    > }
    >
    > So far, everything works fine. When I provide explicit specialization,
    > everything is OK too:
    >
    > template<>
    > int C<int,5>::Member(void) {
    > return 555;
    >
    > }
    >
    > The problem appears, when I try to partially specialize that function:
    >
    > template<class U>
    > int C<U,3>::Member(void) {
    > U u();
    > return 6;
    >
    > }
    >
    > I got some errors under Visual C++ Express:
    >
    > -) error C3860: template argument list following class template name
    > must list parameters in the order used in template parameter list
    > -) error C2995: 'int C<T,I>::Member(void)' : function template has
    > already been defined
    > -) error C2264: 'C<T,I>::Member' : error in function definition or
    > declaration; function not called
    >
    > and under Comeau online compiler:
    >
    > -) "ComeauTest.c", line 21: error: template argument list must match
    > the parameter list
    > int C<U,3>::Member(void) {
    >
    > Is this possible to do in standard C++ way? Thanks for your help.
    >
    > braton


    You cannot partially specialize function or method. it works only for
    classes/structs. As workaround you can implement your method as call
    of static method of some struct and specialize this class:

    template<typename T, int I>
    struct MethodImplementation
    {
    static void Method()
    {
    // Default implementation
    }
    };

    template<typename T>
    struct MethodImplementation<T, 10>
    {
    static void Method()
    {
    // Specialized implementation
    }
    };

    template<typename T, int I>
    class MyClass
    {
    public:
    // ...

    void Method()
    {
    MethodImplementation<T, I>::Method();
    }
    };

    It has no impact on speed of compiled code (all additional methods are
    inlined), but it has worse readability.
    Ondra Holub, Feb 26, 2008
    #3
    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. E11
    Replies:
    1
    Views:
    4,716
    Thomas Weidenfeller
    Oct 12, 2005
  2. Petre Iantu
    Replies:
    1
    Views:
    346
    Petre Iantu
    Aug 17, 2003
  3. SainTiss
    Replies:
    4
    Views:
    396
    SainTiss
    Dec 25, 2003
  4. Dave
    Replies:
    4
    Views:
    7,649
    pdixtl
    Jun 4, 2010
  5. Joseph Turian
    Replies:
    2
    Views:
    457
Loading...

Share This Page