473,396 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Specialisation of member functions possible?

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

Aug 12 '06 #1
2 1439
es*****@googlemail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 12 '06 #2

Victor Bazarov wrote:
es*****@googlemail.com wrote:
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?
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
>
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 12 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Martin MacRobert | last post by:
Hi, I'm trying to make a specialisation of a template function, so that the second parameter accepts scalar types only (int,double,float etc.). How can I do this without writing an explicit...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
2
by: Marc Mutz | last post by:
Hi, I'm currently wondering if and how you can separate definition from declaration of a full template specialisation. I have a UnitTest class that has an _assertEqual member template that I...
1
by: pauljwilliams | last post by:
Consider the following scenario: A template class Diag is defined for a generic class T, with a single member function, State(), returning a boolean, defined as returning false. For some...
8
by: Paul Roberts | last post by:
Hi, I'm hoping somebody here can help me with a simple problem of template syntax. Here's an example: template<typename T, int iclass A { static int a;
8
by: Lionel B | last post by:
On my platform I find that the std::vector<boolspecialisation incurs a significant performance hit in some circumstances (when compared, say, to std::vector<intprogrammed analagously). Is it...
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which...
4
by: Alan Woodland | last post by:
Hi, The following code is legal, and works as expected: #include <iostream> template <typename T> class Bar { };
6
by: johnbrown105 | last post by:
Is it possible to force the compiler to use a generic template rather than a matching specialisation? Consider the following: ////////////////////////////////////////// template.cpp starts...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.