Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 9th, 2006, 06:35 PM
wkaras@yahoo.com
Guest
 
Posts: n/a
Default 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?

  #2  
Old February 9th, 2006, 06:45 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: function vs. class partial specialization

wkaras@yahoo.com wrote:[color=blue]
> 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?[/color]

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
  #3  
Old February 9th, 2006, 08:15 PM
wkaras@yahoo.com
Guest
 
Posts: n/a
Default Re: function vs. class partial specialization

Victor Bazarov wrote:[color=blue]
> wkaras@yahoo.com wrote:[color=green]
> > 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?[/color]
>
> 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.[/color]
....

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.

  #4  
Old February 9th, 2006, 10:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: function vs. class partial specialization

On Thu, 09 Feb 2006 12:01:45 -0800, wkaras wrote:[color=blue]
> Victor Bazarov wrote:[color=green]
>> wkaras@yahoo.com wrote:[color=darkred]
>> > 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?[/color]
>>
>> 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.[/color]
> ...
>
> While necessity is always relative, partial specialization of functions
> could
> be useful:
>
> template <unsigned A, unsigned B>
> void bar(void) { return(foo< (A > B) >()); }[/color]

I don't see any partial specialisation here. Did you mean to define some
'foo' template as well?
[color=blue]
> 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.[/color]

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

  #5  
Old February 10th, 2006, 06:55 AM
Marco Wahl
Guest
 
Posts: n/a
Default Re: function vs. class partial specialization

> template<unsigned U1, unsigned U2> unsigned foo() { return U1+U2; }[color=blue]
> 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;
> }[/color]

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.)

  #6  
Old February 16th, 2006, 12:05 PM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: function vs. class partial specialization

Victor Bazarov wrote:
[color=blue]
> On Thu, 09 Feb 2006 12:01:45 -0800, wkaras wrote:[color=green]
>> Victor Bazarov wrote:[color=darkred]
>>> wkaras@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.[/color]
>> ...
>>
>> While necessity is always relative, partial specialization of functions
>> could
>> be useful:
>>
>> template <unsigned A, unsigned B>
>> void bar(void) { return(foo< (A > B) >()); }[/color]
>
> I don't see any partial specialisation here. Did you mean to define some
> 'foo' template as well?
>[color=green]
>> 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.[/color]
>
> 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.[/color]

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.


  #7  
Old February 16th, 2006, 02:15 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: function vs. class partial specialization

Rolf Magnus wrote:[color=blue]
> [..]
> 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.[/color]

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
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles