Connecting Tech Pros Worldwide Help | Site Map

partially specializing member functions of a template class

Rahul
Guest
 
Posts: n/a
#1: Jul 16 '07
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.

Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 16 '07

re: partially specializing member functions of a template class


Rahul wrote:
Quote:
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.
Quote:
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Craig Scott
Guest
 
Posts: n/a
#3: Jul 16 '07

re: partially specializing member functions of a template class


On Jul 16, 10:25 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Quote:
Rahul wrote:
Quote:
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

Lionel B
Guest
 
Posts: n/a
#4: Jul 16 '07

re: partially specializing member functions of a template class


On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:
Quote:
Rahul wrote:
Quote:
>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
Victor Bazarov
Guest
 
Posts: n/a
#5: Jul 16 '07

re: partially specializing member functions of a template class


Lionel B wrote:
Quote:
On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:
>
Quote:
>Rahul wrote:
Quote:
>>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.
Quote:
>
#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.
Quote:
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


Lionel B
Guest
 
Posts: n/a
#6: Jul 16 '07

re: partially specializing member functions of a template class


On Mon, 16 Jul 2007 09:00:23 -0400, Victor Bazarov wrote:
Quote:
Lionel B wrote:
Quote:
>On Mon, 16 Jul 2007 08:25:20 -0400, Victor Bazarov wrote:
>>
Quote:
>>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.
>
>
Quote:
>#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
Victor Bazarov
Guest
 
Posts: n/a
#7: Jul 16 '07

re: partially specializing member functions of a template class


Lionel B wrote:
Quote:
On Mon, 16 Jul 2007 09:00:23 -0400, Victor Bazarov wrote:
>
Quote:
>Lionel B wrote:
Quote:
>>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.
>>
>>
Quote:
>>#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


Robert Bauck Hamar
Guest
 
Posts: n/a
#8: Jul 16 '07

re: partially specializing member functions of a template class


Lionel B wrote:
Quote:
On Mon, 16 Jul 2007 09:00:23 -0400, Victor Bazarov wrote:
>
Quote:
>Lionel B wrote:
Quote:
>>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.
>>
>>
Quote:
>>#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
Lionel B
Guest
 
Posts: n/a
#9: Jul 16 '07

re: partially specializing member functions of a template class


On Mon, 16 Jul 2007 16:13:33 +0200, Robert Bauck Hamar wrote:
Quote:
Lionel B wrote:
>
Quote:
>On Mon, 16 Jul 2007 09:00:23 -0400, Victor Bazarov wrote:
>>
Quote:
>>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
Closed Thread