By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,290 Members | 1,411 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,290 IT Pros & Developers. It's quick & easy.

partially specializing member functions of a template class

P: n/a
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.

Jul 16 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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
Jul 16 '07 #2

P: n/a
On Jul 16, 10:25 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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

Jul 16 '07 #3

P: n/a
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
Jul 16 '07 #4

P: n/a
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
Jul 16 '07 #5

P: n/a
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-lit....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
Jul 16 '07 #6

P: n/a
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-lit....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
Jul 16 '07 #7

P: n/a
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-lit....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
Jul 16 '07 #8

P: n/a
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-lit....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
Jul 16 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.