Class member specialization

B

braton

Hello

I need some help with template functions specialization. Consider the
following code:

#include <iostream>

template<class T, int I>
class C {
public:
int Member(void);
};

template<class T, int I>
int C<T,I>::Member(void) {
T t();
return I;
}

int main(void) {

C<int,3> c;
std::cout << c.Member() << std::endl;

return 0;
}

So far, everything works fine. When I provide explicit specialization,
everything is OK too:

template<>
int C<int,5>::Member(void) {
return 555;
}

The problem appears, when I try to partially specialize that function:

template<class U>
int C<U,3>::Member(void) {
U u();
return 6;
}

I got some errors under Visual C++ Express:

-) error C3860: template argument list following class template name
must list parameters in the order used in template parameter list
-) error C2995: 'int C<T,I>::Member(void)' : function template has
already been defined
-) error C2264: 'C<T,I>::Member' : error in function definition or
declaration; function not called

and under Comeau online compiler:

-) "ComeauTest.c", line 21: error: template argument list must match
the parameter list
int C<U,3>::Member(void) {

Is this possible to do in standard C++ way? Thanks for your help.

braton
 
V

Victor Bazarov

[..]
The problem appears, when I try to partially specialize that function:
[..]

Is this possible to do in standard C++ way? Thanks for your help.

No. C++ does not allow partial specialisations of template functions.
Use overloading or wrap it into a class and partially-specialise that
class.

V
 
O

Ondra Holub

Hello

I need some help with template functions specialization. Consider the
following code:

#include <iostream>

template<class T, int I>
class C {
public:
int Member(void);

};

template<class T, int I>
int C<T,I>::Member(void) {
T t();
return I;

}

int main(void) {

C<int,3> c;
std::cout << c.Member() << std::endl;

return 0;

}

So far, everything works fine. When I provide explicit specialization,
everything is OK too:

template<>
int C<int,5>::Member(void) {
return 555;

}

The problem appears, when I try to partially specialize that function:

template<class U>
int C<U,3>::Member(void) {
U u();
return 6;

}

I got some errors under Visual C++ Express:

-) error C3860: template argument list following class template name
must list parameters in the order used in template parameter list
-) error C2995: 'int C<T,I>::Member(void)' : function template has
already been defined
-) error C2264: 'C<T,I>::Member' : error in function definition or
declaration; function not called

and under Comeau online compiler:

-) "ComeauTest.c", line 21: error: template argument list must match
the parameter list
int C<U,3>::Member(void) {

Is this possible to do in standard C++ way? Thanks for your help.

braton

You cannot partially specialize function or method. it works only for
classes/structs. As workaround you can implement your method as call
of static method of some struct and specialize this class:

template<typename T, int I>
struct MethodImplementation
{
static void Method()
{
// Default implementation
}
};

template<typename T>
struct MethodImplementation<T, 10>
{
static void Method()
{
// Specialized implementation
}
};

template<typename T, int I>
class MyClass
{
public:
// ...

void Method()
{
MethodImplementation<T, I>::Method();
}
};

It has no impact on speed of compiled code (all additional methods are
inlined), but it has worse readability.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top