Partial Implementation of templates

G

Greg

Hi,

I would like to specify behavior of a class member relatively to
template implemetation. It works in usual cases but it seems to fail
with to templates when one of the two is specified...

Exemple:

template <class T, int Num>
class A
{
void foo ();
}

// The generic implementation is:
template <class T, int Num>
void
A<T,Num>
::foo ()
{
// generic code
}

// I would like to add a specific code when Num equals 2 fior
instance:
template <class T>
void
A<T,2>
::foo()
{
// an other code...
}

// But g++ 3.2.3 (on Redhat Fedora 5) does not like it
Is there any solution, knowing that a complete non-genric code is
possible (i.e. defined a void A<int,2>::foo() directly)?

Any help is appreciated.
Greg
 
O

Ondra Holub

Hi,

I would like to specify behavior of a class member relatively to
template implemetation. It works in usual cases but it seems to fail
with to templates when one of the two is specified...

Exemple:

template <class T, int Num>
class A
{
void foo ();

}

// The generic implementation is:
template <class T, int Num>
void
A<T,Num>
::foo ()
{
// generic code

}

// I would like to add a specific code when Num equals 2 fior
instance:
template <class T>
void
A<T,2>
::foo()
{
// an other code...

}

// But g++ 3.2.3 (on Redhat Fedora 5) does not like it
Is there any solution, knowing that a complete non-genric code is
possible (i.e. defined a void A<int,2>::foo() directly)?

Any help is appreciated.
Greg

Partial specialization works only for classes/structs:

template <class T, int Num>
class A
{
void foo ()
{
// generic code

}

}

template <class T>
class A<T, 2>
{
void foo ()
{
// specialized code

}

}
 
F

Frank Birbacher

Hi!

Ondra said:
Partial specialization works only for classes/structs:

And for non-member functions and template functions of non-template
classes. Right?

They just don't work for partially specializing a templated member
function of a class template. Right?

Is there a link to read up on this issue?

Frank
 
O

Ondra Holub

Hi!



And for non-member functions and template functions of non-template
classes. Right?

I think it works only for classes (and structs, what is the same with
different default access type). You can as workaround create class
with static method and use partial specialization of this class.

AFAIK partial specialization of functions is considered in prepared
standard, but I am not sure.
 
F

Frank Birbacher

Hi!

Ondra said:
AFAIK partial specialization of functions is considered in prepared
standard, but I am not sure.

No. The following already works (not with MSVC prior to 7.1, though):

//generic
template<typename T>
void process(T t) {}

//partial specialization
template<typename T>
void process(T* const t) {}

//full specialization
template<>
void process(void* const t) { throw "what is it?"; }

int main()
{
int i = 8;
process(i);
process(&i);
process( static_cast<void*>(&i) );
}

Frank
 
J

joe

This also seems to work in VC8 anyway and has the advantage of not
having to reimplement the whole of class A:

#include <iostream>

template <class T, int Num>
class A
{
public:
void foo()
{
foo_impl<Num>();
}

private:

template <int N>
void foo_impl()
{
std::cout << "Default Foo" << std::endl;
}
template <>
void foo_impl<2>()
{
std::cout << "Special Foo" << std::endl;
}
};


int main()
{
A<int, 0> a0;
A<int, 2> a2;

a0.foo();
a2.foo();
}

Hope that helps in some way.

joe
 
O

Ondra Holub

Hi!



No. The following already works (not with MSVC prior to 7.1, though):

//generic
template<typename T>
void process(T t) {}

//partial specialization
template<typename T>
void process(T* const t) {}

//full specialization
template<>
void process(void* const t) { throw "what is it?"; }

int main()
{
int i = 8;
process(i);
process(&i);
process( static_cast<void*>(&i) );

}

Frank

Yes. it works. But it is full specialization, not partial
specialization. Try following:

template<typename T, int N>
void Func()
{
}

template<typename T>
void Func<T, 10>()
{
}

On gcc you'll get error: partial specialization `Func<T, 10>' of
function template, on Visual C++ .net 2003 you'll get error C2768:
'Func' : illegal use of explicit template arguments (the same on
Visual C++ 2005).
 
H

hurcan solter

Yes. it works. But it is full specialization, not partial
specialization. Try following:

No it's just a overloaded template function.the last one is a full
specialization though
 
F

Frank Birbacher

Hi!


Is the above really a "full specialization"? o_O
Yes. it works. But it is full specialization, not partial
specialization. Try following:

template<typename T, int N>
void Func()
{
}

template<typename T>
void Func<T, 10>()
{
}

-.- ok, fails. But why? I don't understand this restriction, especially
because classes can be partially specialized.

Frank
 
O

Ondra Holub

Hi!



Is the above really a "full specialization"? o_O

Well, you're right, I missed this one in the middle :)
-.- ok, fails. But why? I don't understand this restriction, especially
because classes can be partially specialized.

Frank

I do not understand it too, but this is current situation :-( I
usually use workaround with static class member which works, although
it looks not as good as simple function:

template<typename T, int N>
class X
{
static void Func() { }
};

template<typename T>
class X<T, 10>
{
void Func<T, 10>() { }
};

Maybe we'll have to wait for a new standard... And then it will depend
on supplier of our compiler :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top