473,545 Members | 2,679 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 2421
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
5765
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...
4
1849
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
1389
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
2753
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...
3
1434
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
2055
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
2906
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
3727
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
8356
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...
0
7685
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7784
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...
0
6014
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...
1
5354
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5071
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
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
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.