473,416 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

partially specializing member functions of a template class

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
8 2406
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
4
by: Old Wolf | last post by:
I have a template class like this: template<typename T, int N> struct Foo { ........... }; which I have successfully specialized for some types, eg:
1
by: mrstephengross | last post by:
Ok, I've got a class with two template parameters (A and B), and a member function foo(). I want to specialize foo for a particular class A. Is this possible? The following code shows an example: ...
9
by: Eric Wang | last post by:
VC 7.1.3088 Bug: When a class defines a template member function taking (T const &), then an explicit specialization taking a non-const & argument (U) causes a C1001 at msc1.cpp line 2701. The...
3
by: PengYu.UT | last post by:
I have the following two program. The first one compiles well, but the second one doesn't. The only difference between them is one more template parameter is added for the second program. Would...
2
by: Simon G Best | last post by:
Hello! I'm trying to specialize a member function template of a class template, like this:- template<typename Tclass thingy { public: template<typename UT f (const U &) const; };
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
5
by: huili80 | last post by:
For example, like in the following, the part commented out was intended as partial spectialzation, but it would even compile. Is it even legal to partially specialize a nested template class...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.