473,466 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

function vs. class partial specialization

I tried a couple of compilers, and both gave errors compiling this:

template <bool fin, typename T>
T foo(T val);

template <typename T>
T foo<true, T>(T val) { return(val); }

But both gave no errors compiling this:

template <bool fin, typename T>
struct foo
{
T operator () (T val);
};

template <typename T>
struct foo<true, T>
{
T operator () (T val) { return(val); }
};
Does the Standard truly have different rules for partial specialization
for function templates vs. class templates? If so, why?

Feb 9 '06 #1
6 2733
wk****@yahoo.com wrote:
I tried a couple of compilers, and both gave errors compiling this:

template <bool fin, typename T>
T foo(T val);

template <typename T>
T foo<true, T>(T val) { return(val); }

But both gave no errors compiling this:

template <bool fin, typename T>
struct foo
{
T operator () (T val);
};

template <typename T>
struct foo<true, T>
{
T operator () (T val) { return(val); }
};
Does the Standard truly have different rules for partial specialization
for function templates vs. class templates? If so, why?


Yes, there are no partial specialisations of function templates. Why?
Ask in comp.std.c++, they own and discuss the rationale behind the C++
Standard. But if I need to make a guess, I'd say, "because it was not
necessary". The same result can be achieved by overloading or by just
defining another function template.

V
--
Please remove capital As from my address when replying by mail
Feb 9 '06 #2
Victor Bazarov wrote:
wk****@yahoo.com wrote:
I tried a couple of compilers, and both gave errors compiling this:

template <bool fin, typename T>
T foo(T val);

template <typename T>
T foo<true, T>(T val) { return(val); }

But both gave no errors compiling this:

template <bool fin, typename T>
struct foo
{
T operator () (T val);
};

template <typename T>
struct foo<true, T>
{
T operator () (T val) { return(val); }
};
Does the Standard truly have different rules for partial specialization
for function templates vs. class templates? If so, why?


Yes, there are no partial specialisations of function templates. Why?
Ask in comp.std.c++, they own and discuss the rationale behind the C++
Standard. But if I need to make a guess, I'd say, "because it was not
necessary". The same result can be achieved by overloading or by just
defining another function template.

....

While necessity is always relative, partial specialization of functions
could
be useful:

template <unsigned A, unsigned B>
void bar(void) { return(foo< (A > B) >()); }

I doesn't occur to me why partial specialization would be considered
more
useful or important for classes as opposed to functions. Of course you
can get around it by defining the equivalent partialy specialized
classes with member functions with the same name. Just seems
like a pointless asymmetry between function and class templates,
though.

Feb 9 '06 #3
On Thu, 09 Feb 2006 12:01:45 -0800, wkaras wrote:
Victor Bazarov wrote:
wk****@yahoo.com wrote:
> I tried a couple of compilers, and both gave errors compiling this:
>
> template <bool fin, typename T>
> T foo(T val);
>
> template <typename T>
> T foo<true, T>(T val) { return(val); }
>
> But both gave no errors compiling this:
>
> template <bool fin, typename T>
> struct foo
> {
> T operator () (T val);
> };
>
> template <typename T>
> struct foo<true, T>
> {
> T operator () (T val) { return(val); }
> };
>
>
> Does the Standard truly have different rules for partial specialization
> for function templates vs. class templates? If so, why?
Yes, there are no partial specialisations of function templates. Why?
Ask in comp.std.c++, they own and discuss the rationale behind the C++
Standard. But if I need to make a guess, I'd say, "because it was not
necessary". The same result can be achieved by overloading or by just
defining another function template.

...

While necessity is always relative, partial specialization of functions
could
be useful:

template <unsigned A, unsigned B>
void bar(void) { return(foo< (A > B) >()); }


I don't see any partial specialisation here. Did you mean to define some
'foo' template as well?
I doesn't occur to me why partial specialization would be considered
more
useful or important for classes as opposed to functions. Of course you
can get around it by defining the equivalent partialy specialized
classes with member functions with the same name. Just seems
like a pointless asymmetry between function and class templates,
though.


Asymetry is due to the different name resolution rules. For example, you
are not allowed to have two different classes with the same name. You are
allowed, however, to have two functions with the same name, but with
different arguments. Why not extend this *existing* asymetry onto
templates? Seems rather natural.

template<unsigned U1, unsigned U2> unsigned foo() { return U1+U2; }
template<unsigned U> unsigned foo() { return U; }
// the latter is instead of
// template<unsigned U> unsigned foo<U,U>() { return U; }
#include <iostream>
int main() {
std::cout << "foo<1,2>() is " << foo<1,2>() << std::endl;
std::cout << "foo<3>() is " << foo<3>() << std::endl;
}

Victor

Feb 9 '06 #4
> template<unsigned U1, unsigned U2> unsigned foo() { return U1+U2; }
template<unsigned U> unsigned foo() { return U; }
// the latter is instead of
// template<unsigned U> unsigned foo<U,U>() { return U; }
#include <iostream>
int main() {
std::cout << "foo<1,2>() is " << foo<1,2>() << std::endl;
std::cout << "foo<3>() is " << foo<3>() << std::endl;
}


At a first glance I thought the above piece of code is just an idea
for function-template overloading. But it indeed compiles and
produces the expected output. (At least with my environment.)

Feb 10 '06 #5
Victor Bazarov wrote:
On Thu, 09 Feb 2006 12:01:45 -0800, wkaras wrote:
Victor Bazarov wrote:
wk****@yahoo.com wrote:
> I tried a couple of compilers, and both gave errors compiling this:
>
> template <bool fin, typename T>
> T foo(T val);
>
> template <typename T>
> T foo<true, T>(T val) { return(val); }
>
> But both gave no errors compiling this:
>
> template <bool fin, typename T>
> struct foo
> {
> T operator () (T val);
> };
>
> template <typename T>
> struct foo<true, T>
> {
> T operator () (T val) { return(val); }
> };
>
>
> Does the Standard truly have different rules for partial
> specialization
> for function templates vs. class templates? If so, why?

Yes, there are no partial specialisations of function templates. Why?
Ask in comp.std.c++, they own and discuss the rationale behind the C++
Standard. But if I need to make a guess, I'd say, "because it was not
necessary". The same result can be achieved by overloading or by just
defining another function template.

...

While necessity is always relative, partial specialization of functions
could
be useful:

template <unsigned A, unsigned B>
void bar(void) { return(foo< (A > B) >()); }


I don't see any partial specialisation here. Did you mean to define some
'foo' template as well?
I doesn't occur to me why partial specialization would be considered
more
useful or important for classes as opposed to functions. Of course you
can get around it by defining the equivalent partialy specialized
classes with member functions with the same name. Just seems
like a pointless asymmetry between function and class templates,
though.


Asymetry is due to the different name resolution rules. For example, you
are not allowed to have two different classes with the same name. You are
allowed, however, to have two functions with the same name, but with
different arguments. Why not extend this *existing* asymetry onto
templates? Seems rather natural.


I'd ask the opposite question: What harm would be done by treating class
templates and function templates equally regarding partial specialization?
That would seem more natural to me than adding an artificial inconsistency
just because there is another way for functions.
Feb 16 '06 #6
Rolf Magnus wrote:
[..]
I'd ask the opposite question: What harm would be done by treating class
templates and function templates equally regarding partial specialization?
That would seem more natural to me than adding an artificial inconsistency
just because there is another way for functions.


Wouldn't 'comp.std.c++' be a better place to ask this question?

V
--
Please remove capital As from my address when replying by mail
Feb 16 '06 #7

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: SainTiss | last post by:
Hi, From what I've read in several places, it seems that explicit specialization of member functions of class templates is allowed, but partial specialization isn't: template<class T, class...
1
by: SainTiss | last post by:
Hi, I've been looking into the standard for a clear statement on whether partial specialization of member functions of class templates is allowed or not. 14.7.3/4 says that explicit...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
2
by: Michael Stembera | last post by:
Here is a very simple piece of code to repro this bug. template<typename T, int N> inline bool foo( void ) { return true; } template<typename T> inline bool foo<T, 1>( void ) { return...
2
by: lhr_cool_guy | last post by:
C++ doesn't allow partial specialization of function templates. I want to know why this is so? Is the concept of function template partial specialization not well defined? I have a case where I...
5
by: desktop | last post by:
I have this example: template<class T(1) void f( T ); template<class T(2) void f( T* ); template< (3)
7
by: mathieu | last post by:
Hi there, I know this is not possible in c++. So my question, how should I rewrite the following piece of code (without using a dummy class which template parameter could be use for partial...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
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
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
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...
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.