473,837 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<"special ization\n"; }

But the above doesn't work and fails during compilation.

Jul 16 '07 #1
8 2444
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...@com Acast.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
5782
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
4
1862
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
1401
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: ===================================== template<typename A, typename B> class Foo { int foo() { return 5; } };
9
2763
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 following short program duplicates this error. Compile it in Debug mode (all optimizations disabled), with "Not using precompiled headers". Class C defines F as a template member function taking (_T const &). It then specializes F taking a char....
3
1447
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 you please help on how to make the second program work? Thanks, Peng /************first************************/
2
2070
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
2927
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 T,typename Alloc = std::allocator<T class CircularBuffer4 { public: typedef typename CircularBuffer<T,Alloc>::size_type
5
3744
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 inside another template class? template < typename T > struct A { template < typename U > struct B
17
8391
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 the function if possible" (because if the compiler has the function definition available at an instantiation point, it will estimate whether to inline it or not, and do so if it estimates it would be beneficial, completely regardless of whether...
0
9843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9682
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10575
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10628
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10275
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9406
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5850
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4475
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4043
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.