Specialisation of member functions possible?

E

esuvs81

Hi guys, is it possible to use partial specialization on individual
member functions of a class (rather than partially specializing the
whole class)? I have the following code:

#include <iostream>

template<typename T, typename U>
class MyClass
{
public:
void func(void);
};

template<typename T, typename U>
void MyClass<T,U>::func(void)
{
std::cout << "In generic function" << std::endl;
}

template<>
void MyClass<float,char>::func(void)
{
std::cout << "In function specialized on both types" << std::endl;
}

template<> template<typename U>
void MyClass<int,U>::func(void)
{
std::cout << "In function specialized on one type" << std::endl;
}

The last function doesn't work, the compiler gives me the following
error:

error C2244: 'MyClass<T,U>::func' : unable to match function definition
to an existing declaration

Is it just a syntactic error on my part, or is this prohibited fior
some reason.

To place it in context the real problem occurs in my Matrix class which
is paramatised on both size and data type. Most of the functions
(multiplication, transpose, etc) are generic but 2D, 3D, and 4D
rotation matrices are generated in different ways.

Anyway, any help appriciated,

David
 
V

Victor Bazarov

Hi guys, is it possible to use partial specialization on individual
member functions of a class (rather than partially specializing the
whole class)? I have the following code:

#include <iostream>

template<typename T, typename U>
class MyClass
{
public:
void func(void);
};

template<typename T, typename U>
void MyClass<T,U>::func(void)
{
std::cout << "In generic function" << std::endl;
}

template<>
void MyClass<float,char>::func(void)
{
std::cout << "In function specialized on both types" << std::endl;
}

Yes, this is OK.
template<> template<typename U>
void MyClass<int,U>::func(void)
{
std::cout << "In function specialized on one type" << std::endl;
}

No, this is not OK.
The last function doesn't work, the compiler gives me the following
error:

error C2244: 'MyClass<T,U>::func' : unable to match function
definition to an existing declaration

Is it just a syntactic error on my part, or is this prohibited fior
some reason.

Both. To partially specialise a class (and that's what you are
trying there), there is a certain syntax. You cannot partially
specialise a single member.
To place it in context the real problem occurs in my Matrix class
which is paramatised on both size and data type. Most of the functions
(multiplication, transpose, etc) are generic but 2D, 3D, and 4D
rotation matrices are generated in different ways.

There probably is a different approach to making it work. What if
you divise a separate "rotation matrix generator" and use it?

V
 
E

esuvs81

Victor said:
Yes, this is OK.


No, this is not OK.


Both. To partially specialise a class (and that's what you are
trying there), there is a certain syntax. You cannot partially
specialise a single member.


There probably is a different approach to making it work. What if
you divise a separate "rotation matrix generator" and use it?

Ok, thanks. I half suspected it wasn't possible because I couldn't find
any examples, but then again I couldn't think of any technical reason
why it shouldn't be possible (not that I know much about that side of
things). But yes, a seperate rotation function generator matrix is
probably what i'll use.

Thanks again,

David
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top