Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > partial specialization of template member function

Reply
Thread Tools

partial specialization of template member function

 
 
Levent
Guest
Posts: n/a
 
      05-10-2005
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>
void Foo<T,N>::func() {
std::cout << "Primary -- "
<< "T is " << typeid(T).name() << " -- "
<< "N=" << N
<< std::endl;
}
template<class T>
void Foo<T,3>::func() {
std::cout << "Specialized for N=3 -- "
<< "T is " << typeid(T).name() << " -- "
<< std::endl;
}

int main() {
Foo<double,3> d;
Foo<int,5> i;
d.func();
i.func();
return 0;
}

gcc compiler error: no `void Foo<T, 3>::func()' member function declared in
class `Foo<T, 3>'
(VC7.1 error is similar)

On the other hand, full specialization of the member works (without
specializing the whole class). (try replacing `template<class T> void
Foo<T,3>::func()' with `template<> void Foo<double,3>::func()')

If this behaviour is how it is defined in the standard, what may be a
possible workaround to achieve partial specialization on member functions?

thank you
- sly.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-10-2005
Levent wrote:
> Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1):
> [..]


Because you have to first define the partial specialisation of the
enclosing class template.

> On the other hand, full specialization of the member works (without
> specializing the whole class). (try replacing `template<class T> void
> Foo<T,3>::func()' with `template<> void Foo<double,3>::func()')
>
> If this behaviour is how it is defined in the standard, what may be a
> possible workaround to achieve partial specialization on member functions?


This is as specified. There are no partial specialisations of function
templates, and to partially specialise a member you need to first
partially specialise the class template.

V
 
Reply With Quote
 
 
 
 
Levent
Guest
Posts: n/a
 
      05-10-2005
> VB: There are no partial specialisations of function templates ....


When you say this you do not mean any function, right? 'cause non-member
function templates *can* be specialized. And I should admit that partial
specialization of member function w/o partial spec of full class is
somewhat ill-conditioned design (can't explain why, just fells like that
way...).

However, I came up with a workaround: Use non-member friend template
functions.

Before listing the code that does this I want to note a gotcha of
VC++7.1 (actually this is somewhat the reason I am posting this
follow-up) which probably is a good example of how unreliable this
compiler is.

Here is how I implemented non-member friend template (tried to trim as
much as I can):

================================================== =
#include <iostream>

using namespace std;

template<class T, unsigned N> class Foo;
template<class T, unsigned N> void print(Foo<T,N> &f);

template<class T, unsigned N>
class Foo {
int i;
public:
friend void print<>(Foo &);
Foo(int i_) :i(i_) {};
};

template<class T, unsigned N>
void print(Foo<T,N> &f) {
cout << "Foo<" << typeid(T).name() << ", " << N << ">"
<< ".i=" << f.i << endl;
}
template<class T>
void print(Foo<T,4> &f) {
cout
<< "Specialized for N=4: "
<< "Foo<" << typeid(T).name() << ", " << 4 << ">"
<< ".i=" << f.i << endl;
}

int main() {
Foo<int,4> a(9); // specialized <> instantiation
// Foo<double,4> b(9); // another specialized <> instantiation
Foo<double,5> c(10); // primary <> instantiation
print(a);
// print(b);
print(c);
return 0;
}
================================================== ===

This compiles (and runs) fine with gcc 3.3.3 and intel 8.1.

However, with VC++7.1, it gives `print cannot access private member'
compile error.

Even worse happens: Commentting out the 1st and 4th lines of main() and
uncommenting 2nd and 5th, VC++ _does_ compile... But very wrong things
happen during run: c acts as if Foo<double,5> !!!

can anyone confirm that this is the case?

Victor Bazarov wrote:

> Levent wrote:
>
>> Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1):
>> [..]

>
>
> Because you have to first define the partial specialisation of the
> enclosing class template.
>
>> On the other hand, full specialization of the member works (without
>> specializing the whole class). (try replacing `template<class T>
>> void Foo<T,3>::func()' with `template<> void Foo<double,3>::func()')
>>
>> If this behaviour is how it is defined in the standard, what may be a
>> possible workaround to achieve partial specialization on member
>> functions?

>
>
> This is as specified. There are no partial specialisations of function
> templates, and to partially specialise a member you need to first
> partially specialise the class template.
>
> V

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-10-2005
Levent wrote:
>> VB: There are no partial specialisations of function templates ....

>
>
>
> When you say this you do not mean any function, right? 'cause non-member
> function templates *can* be specialized.


Not partially. Only fully. There are no partial specialisations of
function templates. Period. And when I say this I mean any function,
member or non-member.

You can partially specialise a class template. You can then define
a member of that specialisation. That's not the same as partially
specialising a function template.

> [...]


V
 
Reply With Quote
 
Levent
Guest
Posts: n/a
 
      05-10-2005
What I meant was *partial* specializations of template functions are
possible. Please, take a look at this (valid) code:

using namespace std;
template<class T1, class T2>
void foobar(T1 &t1,T2 &t2) {
cout << "Primary <> instantiation -- foobar<"
<< typeid(t1).name() << "," << typeid(t2).name() << ">"
<< endl;
}
template<class T>
void foobar(T &t1,double &t2) {
cout << "Specialized <> instantiation -- foobar<"
<< typeid(t1).name() << "," << typeid(double).name() << ">"
<< endl;
}

int main() {
double k,l; int i,j;
foobar(i,j);
foobar(k,j);
foobar(i,k);
return 0;
}

This obviously works. But maybe it is my confusion with terminology:
What I assume as specialization is nothing but overloading, somewhat ...

If you are referring to the standard when you assert that

> There are no partial specialisations of function templates. Period.


I should take your word for it... But then, isn't it the case that
explicit (full) specialization, is also in fact overloading?

BTW, what do you think of the code in my previous post? Don't you think
it is interesting that VC++7.1 fails with this?

Victor Bazarov wrote:

> Levent wrote:
>
>>> VB: There are no partial specialisations of function templates ....

>>
>>
>>
>>
>> When you say this you do not mean any function, right? 'cause
>> non-member function templates *can* be specialized.

>
>
> Not partially. Only fully. There are no partial specialisations of
> function templates. Period. And when I say this I mean any function,
> member or non-member.
>
> You can partially specialise a class template. You can then define
> a member of that specialisation. That's not the same as partially
> specialising a function template.
>
> > [...]

>
> V

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-10-2005
Levent wrote:
> What I meant was *partial* specializations of template functions are
> possible. Please, take a look at this (valid) code:
>
> using namespace std;
> template<class T1, class T2>
> void foobar(T1 &t1,T2 &t2) {
> cout << "Primary <> instantiation -- foobar<"
> << typeid(t1).name() << "," << typeid(t2).name() << ">"
> << endl;
> }
> template<class T>
> void foobar(T &t1,double &t2) {
> cout << "Specialized <> instantiation -- foobar<"
> << typeid(t1).name() << "," << typeid(double).name() << ">"
> << endl;
> }


This is not a specialisation. It's just another template. If it were
a specialisation, it would have <???> after the function name.

> int main() {
> double k,l; int i,j;
> foobar(i,j);
> foobar(k,j);
> foobar(i,k);
> return 0;
> }
>
> This obviously works. But maybe it is my confusion with terminology:
> What I assume as specialization is nothing but overloading, somewhat ...


Exactly. Two different templates, both contribute to overloading
resolution.

> If you are referring to the standard when you assert that
>
>> There are no partial specialisations of function templates. Period.

>
>
> I should take your word for it... But then, isn't it the case that
> explicit (full) specialization, is also in fact overloading?


Yes.

> BTW, what do you think of the code in my previous post? Don't you think
> it is interesting that VC++7.1 fails with this?


I didn't look.

V

P.S. Please don't top-post.
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to do partial specialization template for non member method template vj C++ 1 12-20-2010 11:57 AM
[partial] template specialization for a non-member function puzzlecracker C++ 2 01-29-2009 09:19 AM
syntax problem for partial template specialization for a template class. toton C++ 1 12-28-2006 04:08 PM
member template partial specialization Kai-Uwe Bux C++ 1 12-09-2005 02:45 PM
Partial member of class template specialization Petre Iantu C++ 1 08-17-2003 06:10 PM



Advertisments