partially specializing member functions of a template class

R

Rahul

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.
 
C

Craig Scott

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.
 
L

Lionel B

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
 
V

Victor Bazarov

Lionel said:
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
 
L

Lionel B

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.

[...]
 
V

Victor Bazarov

Lionel said:
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
 
R

Robert Bauck Hamar

Lionel said:
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.
 
L

Lionel B

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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top