partially specializing member functions of a template class

Discussion in 'C++' started by Rahul, Jul 16, 2007.

  1. Rahul

    Rahul Guest

    Hi,

    Is there a way to partially specialize only a member function of a
    template class (not the whole class).
    e.g.

    template <typename A, typename B>
    class Base
    {
    public:
    void fun (int key ) {cout<<"inside the template class\n"; }
    };

    template <class A>
    void Base<A, int>::fun ( int key )
    {cout<<"specialization\n"; }

    But the above doesn't work and fails during compilation.
     
    Rahul, Jul 16, 2007
    #1
    1. Advertising

  2. Rahul wrote:
    > Is there a way to partially specialize only a member function


    There is no way to partially specialize _any_ template function.
    The only thing you can partially specialize is a class template.

    > [..]


    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, Jul 16, 2007
    #2
    1. Advertising

  3. Rahul

    Craig Scott Guest

    On Jul 16, 10:25 pm, "Victor Bazarov" <> wrote:
    > Rahul wrote:
    > > Is there a way to partially specialize only a member function

    >
    > There is no way to partially specialize _any_ template function.
    > The only thing you can partially specialize is a class template.


    True, but it is trivial to use a class to make the template function
    effectively partially specialized. Just have it forward to a static
    function within a template class and partially specialize that
    template class. I admit its a bit verbose and seems like more work
    than should be necessary, but it works just fine. Naturally, there are
    variations on this theme to suit different situations.

    --
    Computational Modeling, CSIRO (CMIS)
    Melbourne, Australia
     
    Craig Scott, Jul 16, 2007
    #3
  4. Rahul

    Lionel B Guest

    On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:

    > Rahul wrote:
    >> Is there a way to partially specialize only a member function

    >
    > There is no way to partially specialize _any_ template function. The
    > only thing you can partially specialize is a class template.


    Huh? Doesn't this count as a partial specialisation of a template
    function?

    #include <iostream>

    template <typename T1, typename T2>
    void foo(const T1& t1, const T2& t2)
    {
    std::cout << "non-specialised\n";
    std::cout << "t1 = " << t1 << '\n';
    std::cout << "t2 = " << t2 << '\n';
    }

    template <typename T1>
    void foo(const T1& t1, const int& t2)
    {
    std::cout << "partially specialised\n";
    std::cout << "t1 = " << t1 << '\n';
    std::cout << "t2 = " << t2 << '\n';
    }

    int main()
    {
    foo(6.7, "hallo");
    foo(6.7,3);
    }

    Output:

    non-specialised
    t1 = 6.7
    t2 = hallo
    partially specialised
    t1 = 6.7
    t2 = 3


    --
    Lionel B
     
    Lionel B, Jul 16, 2007
    #4
  5. Lionel B wrote:
    > On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:
    >
    >> Rahul wrote:
    >>> Is there a way to partially specialize only a member function

    >>
    >> There is no way to partially specialize _any_ template function. The
    >> only thing you can partially specialize is a class template.

    >
    > Huh? Doesn't this count as a partial specialisation of a template
    > function?


    No.

    >
    > #include <iostream>
    >
    > template <typename T1, typename T2>
    > void foo(const T1& t1, const T2& t2)
    > {
    > std::cout << "non-specialised\n";
    > std::cout << "t1 = " << t1 << '\n';
    > std::cout << "t2 = " << t2 << '\n';
    > }
    >
    > template <typename T1>
    > void foo(const T1& t1, const int& t2)
    > {
    > std::cout << "partially specialised\n";


    Printing out "partially specialised" in a function does not make
    that function partially specialised, sorry.

    What you have here is an *overloaded* function template.

    > std::cout << "t1 = " << t1 << '\n';
    > std::cout << "t2 = " << t2 << '\n';
    > }
    >
    > int main()
    > {
    > foo(6.7, "hallo");
    > foo(6.7,3);
    > }
    >
    > Output:
    >
    > non-specialised
    > t1 = 6.7
    > t2 = hallo
    > partially specialised
    > t1 = 6.7
    > t2 = 3


    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, Jul 16, 2007
    #5
  6. Rahul

    Lionel B Guest

    On Mon, 16 Jul 2007 09:00:23 -0400, Victor Bazarov wrote:

    > Lionel B wrote:
    >> On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:
    >>
    >>> Rahul wrote:
    >>>> Is there a way to partially specialize only a member function
    >>>
    >>> There is no way to partially specialize _any_ template function. The
    >>> only thing you can partially specialize is a class template.

    >>
    >> Huh? Doesn't this count as a partial specialisation of a template
    >> function?

    >
    > No.
    >
    >
    >> #include <iostream>
    >>
    >> template <typename T1, typename T2>
    >> void foo(const T1& t1, const T2& t2)
    >> {
    >> std::cout << "non-specialised\n";
    >> std::cout << "t1 = " << t1 << '\n';
    >> std::cout << "t2 = " << t2 << '\n';
    >> }
    >>
    >> template <typename T1>
    >> void foo(const T1& t1, const int& t2) {
    >> std::cout << "partially specialised\n";

    >
    > Printing out "partially specialised" in a function does not make that
    > function partially specialised, sorry.
    >
    > What you have here is an *overloaded* function template.


    Interesting. The example in the FAQ:

    http://www.parashift.com/c -faq-lite/templates.html#faq-35.7

    is admittedly not "partial" specialisation, but, assuming that it
    represents correct usage of the term "function template specialisation",
    are you implying that as soon as we specialise/overload/whatever-you-call-
    it on only *one* of several template arguments it ceases to be
    specialisation at all and becomes "function template overloading"? That
    seems a bit cranky terminology-wise to me.

    [...]

    --
    Lionel B
     
    Lionel B, Jul 16, 2007
    #6
  7. Lionel B wrote:
    > On Mon, 16 Jul 2007 09:00:23 -0400, Victor Bazarov wrote:
    >
    >> Lionel B wrote:
    >>> On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:
    >>>
    >>>> Rahul wrote:
    >>>>> Is there a way to partially specialize only a member function
    >>>>
    >>>> There is no way to partially specialize _any_ template function.
    >>>> The only thing you can partially specialize is a class template.
    >>>
    >>> Huh? Doesn't this count as a partial specialisation of a template
    >>> function?

    >>
    >> No.
    >>
    >>
    >>> #include <iostream>
    >>>
    >>> template <typename T1, typename T2>
    >>> void foo(const T1& t1, const T2& t2)
    >>> {
    >>> std::cout << "non-specialised\n";
    >>> std::cout << "t1 = " << t1 << '\n';
    >>> std::cout << "t2 = " << t2 << '\n';
    >>> }
    >>>
    >>> template <typename T1>
    >>> void foo(const T1& t1, const int& t2) {
    >>> std::cout << "partially specialised\n";

    >>
    >> Printing out "partially specialised" in a function does not make that
    >> function partially specialised, sorry.
    >>
    >> What you have here is an *overloaded* function template.

    >
    > Interesting. The example in the FAQ:
    >
    > http://www.parashift.com/c -faq-lite/templates.html#faq-35.7
    >
    > is admittedly not "partial" specialisation, but, assuming that it
    > represents correct usage of the term "function template
    > specialisation", are you implying that as soon as we
    > specialise/overload/whatever-you-call- it on only *one* of several
    > template arguments it ceases to be specialisation at all and becomes
    > "function template overloading"? That seems a bit cranky
    > terminology-wise to me.


    IIRC, different name lookup and argument deduction rules apply,
    that's why it's in a different category than "specialization".
    But don't ask me to explain them, refer instead to the great book
    by Vandevoorde and Josuttis on C++ templates.

    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, Jul 16, 2007
    #7
  8. Lionel B wrote:

    > On Mon, 16 Jul 2007 09:00:23 -0400, Victor Bazarov wrote:
    >
    >> Lionel B wrote:
    >>> On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:
    >>>
    >>>> Rahul wrote:
    >>>>> Is there a way to partially specialize only a member function
    >>>>
    >>>> There is no way to partially specialize _any_ template function. The
    >>>> only thing you can partially specialize is a class template.
    >>>
    >>> Huh? Doesn't this count as a partial specialisation of a template
    >>> function?

    >>
    >> No.
    >>
    >>
    >>> #include <iostream>
    >>>
    >>> template <typename T1, typename T2>
    >>> void foo(const T1& t1, const T2& t2)
    >>> {
    >>> std::cout << "non-specialised\n";
    >>> std::cout << "t1 = " << t1 << '\n';
    >>> std::cout << "t2 = " << t2 << '\n';
    >>> }
    >>>
    >>> template <typename T1>
    >>> void foo(const T1& t1, const int& t2) {
    >>> std::cout << "partially specialised\n";

    >>
    >> Printing out "partially specialised" in a function does not make that
    >> function partially specialised, sorry.
    >>
    >> What you have here is an *overloaded* function template.

    >
    > Interesting. The example in the FAQ:
    >
    > http://www.parashift.com/c -faq-lite/templates.html#faq-35.7
    >
    > is admittedly not "partial" specialisation, but, assuming that it
    > represents correct usage of the term "function template specialisation",
    > are you implying that as soon as we specialise/overload/whatever-you-call-
    > it on only *one* of several template arguments it ceases to be
    > specialisation at all and becomes "function template overloading"? That
    > seems a bit cranky terminology-wise to me.


    Function templates can be overloaded with other function templates and with
    other functions. That's what happening here. There is no partial
    specialisation of function templates.

    Vandevoorde and Josuttis writes in /C++ Templates: The Complete Guide/:

    You may legitimately wonder why only class templates can be partially
    specialized. The reasons are mostly historical. It is probably possible to
    define the same mechanism for function templates (see Chapter 13). In some
    ways the effect of overloading function templates is similar, but there are
    also some subtle differences. These differences are mostly related to the
    fact that only the primary template needs to be looked up when a use is
    encountered. The specializations are considered only afterward, to
    determine which implementation should be used. In contrast, all overloaded
    function templates must be brought into an overload set by looking them up,
    and they may come from different namespaces or classes. This increases the
    likelihood of unintentionally overloading a template name somewhat.

    Chapter 13 discusses future directions.
    --
    rbh
     
    Robert Bauck Hamar, Jul 16, 2007
    #8
  9. Rahul

    Lionel B Guest

    On Mon, 16 Jul 2007 16:13:33 +0200, Robert Bauck Hamar wrote:

    > Lionel B wrote:
    >
    >> On Mon, 16 Jul 2007 09:00:23 -0400, Victor Bazarov wrote:
    >>
    >>> Lionel B wrote:
    >>>> On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:
    >>>>
    >>>>> Rahul wrote:
    >>>>>> Is there a way to partially specialize only a member function
    >>>>>
    >>>>> There is no way to partially specialize _any_ template function. The
    >>>>> only thing you can partially specialize is a class template.
    >>>>
    >>>> Huh? Doesn't this count as a partial specialisation of a template
    >>>> function?
    >>>
    >>> No.
    >>>
    >>>
    >>>> #include <iostream>
    >>>>
    >>>> template <typename T1, typename T2>
    >>>> void foo(const T1& t1, const T2& t2)
    >>>> {
    >>>> std::cout << "non-specialised\n";
    >>>> std::cout << "t1 = " << t1 << '\n';
    >>>> std::cout << "t2 = " << t2 << '\n';
    >>>> }
    >>>>
    >>>> template <typename T1>
    >>>> void foo(const T1& t1, const int& t2) { std::cout << "partially
    >>>> specialised\n";
    >>>
    >>> Printing out "partially specialised" in a function does not make that
    >>> function partially specialised, sorry.
    >>>
    >>> What you have here is an *overloaded* function template.

    >>
    >> Interesting. The example in the FAQ:
    >>
    >> http://www.parashift.com/c -faq-lite/templates.html#faq-35.7
    >>
    >> is admittedly not "partial" specialisation, but, assuming that it
    >> represents correct usage of the term "function template
    >> specialisation", are you implying that as soon as we
    >> specialise/overload/whatever-you-call- it on only *one* of several
    >> template arguments it ceases to be specialisation at all and becomes
    >> "function template overloading"? That seems a bit cranky
    >> terminology-wise to me.

    >
    > Function templates can be overloaded with other function templates and
    > with other functions. That's what happening here. There is no partial
    > specialisation of function templates.
    >
    > Vandevoorde and Josuttis writes in /C++ Templates: The Complete Guide/:
    >
    > You may legitimately wonder why only class templates can be partially
    > specialized. The reasons are mostly historical. It is probably possible
    > to define the same mechanism for function templates (see Chapter 13). In
    > some ways the effect of overloading function templates is similar, but
    > there are also some subtle differences. These differences are mostly
    > related to the fact that only the primary template needs to be looked up
    > when a use is encountered. The specializations are considered only
    > afterward, to determine which implementation should be used. In
    > contrast, all overloaded function templates must be brought into an
    > overload set by looking them up, and they may come from different
    > namespaces or classes. This increases the likelihood of unintentionally
    > overloading a template name somewhat.
    >
    > Chapter 13 discusses future directions.


    Phew. I'll chew on that... another time (my brain is not doing "subtle"
    today).

    Thanks,

    --
    Lionel B
     
    Lionel B, Jul 16, 2007
    #9
    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. Jeff
    Replies:
    2
    Views:
    420
  2. Simon G Best
    Replies:
    2
    Views:
    558
    Simon G Best
    Dec 29, 2006
  3. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    418
    Michael DOUBEZ
    Apr 24, 2009
  5. m0shbear
    Replies:
    2
    Views:
    349
Loading...

Share This Page