473,800 Members | 2,414 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me T, typename U>
class MyClass
{
public:
void func(void);
};

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

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

template<templa te<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 1461
es*****@googlem ail.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<typena me T, typename U>
class MyClass
{
public:
void func(void);
};

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

template<>
void MyClass<float,c har>::func(void )
{
std::cout << "In function specialized on both types" << std::endl;
}
Yes, this is OK.
>
template<templa te<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*****@googlem ail.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<typena me T, typename U>
class MyClass
{
public:
void func(void);
};

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

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

Yes, this is OK.

template<templa te<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
2792
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 specialisation for all scalar types? This is because of the large number of functions to overload. For example:
7
2134
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
1332
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 want to specialize for types from a 3rd-party library. Simplified, the code looks like this:
1
1299
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 types T, the template is specialized such that State returns true. In this sense, the template class as akin to a policy class. At compile time, how does the compiler search the source tree for all
8
3861
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
5376
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 possible to "spoof" std::vector into implementing a "true" vector of bool rather than the specialisation? Say I do: typedef bool boolreally; std::vector<booleallybvec;
6
3508
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 can be used by static member functions of the base class? Something like virtual static XClass* pXClass; /* pXClass shall be pure virtual, so not static in base class, but
4
1363
by: Alan Woodland | last post by:
Hi, The following code is legal, and works as expected: #include <iostream> template <typename T> class Bar { };
6
1640
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 here #include <iostream> #include <ostream>
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.