473,721 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

specialize template member function of a template class

Hi,
I want to specialize template member function of a template class .
It is creating some syntax problem ....
Can anyone say how to do it ?

The class is something like this
template<typena me T,typename Alloc = std::allocator< T
class CircularBuffer4 {
public:
typedef typename CircularBuffer< T,Alloc>::size_ type
size_type;
private:
CircularBuffer< T,Allocx_;
CircularBuffer< T,Allocy_;
CircularBuffer< T,Allocxd_;
CircularBuffer< T,Allocyd_;
template<Direct ionType dtconst CircularBuffer< T,Alloc>& buf()const;
}
where
enum DirectionType{
dtX,dtY
};
I want to specialize buf for dtX & dtY

template<typena me T,typename Alloc>
template<>const CircularBuffer< T,Alloc>&
CircularBuffer4 <T,Alloc>::buf< dtX>()const{
return x_;
}
This does not compile,
but,
template<typena me T,typename Alloc>
template<Direct ionType dt>const CircularBuffer< T,Alloc>&
CircularBuffer4 <T,Alloc>::buf( )const{
return x_;
}
this compiles ....
In both cases all of the codes are in header (I hadn't made the
specialized template code in a cpp file, hope that doesn't cause
problem)

thanks in advance
abir

Jan 24 '07 #1
3 2918
toton wrote:
I want to specialize template member function of a template class .
It is creating some syntax problem ....
Can anyone say how to do it ?
In order to specialise a member you _must_ specialise the class first.
The class is something like this
template<typena me T,typename Alloc = std::allocator< T
class CircularBuffer4 {
public:
typedef typename CircularBuffer< T,Alloc>::size_ type
size_type;
private:
CircularBuffer< T,Allocx_;
CircularBuffer< T,Allocy_;
CircularBuffer< T,Allocxd_;
CircularBuffer< T,Allocyd_;
template<Direct ionType dtconst CircularBuffer< T,Alloc>& buf()const;
}
where
enum DirectionType{
dtX,dtY
};
I want to specialize buf for dtX & dtY
Why? How would you use it? Couldn't you simply have two functions,
one named 'buf_dtX' and the other 'buf_dtY'?
template<typena me T,typename Alloc>
template<>const CircularBuffer< T,Alloc>&
CircularBuffer4 <T,Alloc>::buf< dtX>()const{
return x_;
}
This does not compile,
but,
template<typena me T,typename Alloc>
template<Direct ionType dt>const CircularBuffer< T,Alloc>&
CircularBuffer4 <T,Alloc>::buf( )const{
return x_;
}
this compiles ....
Because it's not a specialisation. It's just the definition.
In both cases all of the codes are in header (I hadn't made the
specialized template code in a cpp file, hope that doesn't cause
problem)
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 24 '07 #2


On Jan 24, 7:25 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
toton wrote:
I want to specialize template member function of a template class .
It is creating some syntax problem ....
Can anyone say how to do it ?In order to specialise a member you _must_ specialise the class first.


The class is something like this
template<typena me T,typename Alloc = std::allocator< T
class CircularBuffer4 {
public:
typedef typename CircularBuffer< T,Alloc>::size_ type
size_type;
private:
CircularBuffer< T,Allocx_;
CircularBuffer< T,Allocy_;
CircularBuffer< T,Allocxd_;
CircularBuffer< T,Allocyd_;
template<Direct ionType dtconst CircularBuffer< T,Alloc>& buf()const;
}
where
enum DirectionType{
dtX,dtY
};
I want to specialize buf for dtX & dtY
"Why? How would you use it? Couldn't you simply have two functions,"
Yes, two function is definitely possible. Just wanted to add little
"sugar" to it.
I have a templated function operate, which operates on dtX & dtY. Both
are called, always.
Thus either I have to write 2 operate functions, or generate 2, if I
dont want to use if else ....
So I generated. Now once operate is generated, I need have template
member as all of these class which it calls, again to avoid if else.

The code flow is something like this (all classes are not defined, but
surely one will get the flow).
void SegmentOperator ::operate(store ::CC& cc) {
cc_ = &cc;
store::ConstPoi ntRange points = cc.points();
operate<dtX>(po ints);
operate<dtY>(po ints);
operate<dtXD>(p oints);
operate<dtYD>(p oints);
}
well, I could have written 4 functions instead of generating, or a
single function with a if -else or switch statement.

each template are something like,
template<Direct ionType dt>void
SegmentOperator ::operate(store ::ConstPointRan ge& points) {
size_t size = points.size();
//Current CC reconstructed Points.
Range range(0,size);
//check the No. of points in CC.
//if No. of points less than 2 than nothing to do.
if (size < 2) {
cc_->addSegment<dt> (store::Segment (cc_,range,stDo t));
return;
}
///blah blah blah...
}
Here ConstPointRange is a boost::sub_rang e (a pair of iterators) ,
Range is typedef of std::pair, Segment & CC are my specialized class.
Here as all of the operations are done in 4 directions always,
sometimes 4 specialization, but mostly through simple template ...
That avoids a switch and throw at default value (a runtime check) , and
makes sure all of the parameters passed as direction are proper.
Now once this is templated, whatever it calls can be templated also.
Like I can write a if with addSegment_x, addSegment_y etc, or even
addSegment(Dire ctionType dt) . In all cases if-else or switch with
throw is there.

Now it also stores 4 directional data , in form of vector4 or
circular_buffer 4 , and each operator needs to take the appropriate
data.
Of course each of the type for the container is specified, using
typedef like typedef PointVector4 Vector4<Point, but I thought to
implement the templated member for vector4 instead, and use if for all
PointVector4, VelocityVector4 etc.

At present I have a switch at the templated classes, with non
templated member function, but for a non templated class a templated
member function.
I have a bunch of Vector4 classes which are typedef. If I want to
specialize all of them, it will again cause repetition. Rather I want
to tell the compiler that these typedefs are specialization, and make
the 4 template member function for only those typedefs.

I hope, it says my problem to some extent.
Thanks for pleasant listening ....
looking for some advice.

abir

one named 'buf_dtX' and the other 'buf_dtY'?
template<typena me T,typename Alloc>
template<>const CircularBuffer< T,Alloc>&
CircularBuffer4 <T,Alloc>::buf< dtX>()const{
return x_;
}
This does not compile,
but,
template<typena me T,typename Alloc>
template<Direct ionType dt>const CircularBuffer< T,Alloc>&
CircularBuffer4 <T,Alloc>::buf( )const{
return x_;
}
this compiles ....Because it's not a specialisation. It's just the definition.
In both cases all of the codes are in header (I hadn't made the
specialized template code in a cpp file, hope that doesn't cause
problem)V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 25 '07 #3
toton wrote:
On Jan 24, 7:25 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
>toton wrote:
>> I want to specialize template member function of a template class .
It is creating some syntax problem ....
Can anyone say how to do it ?In order to specialise a member you
_must_ specialise the class first.
[...]

Take a look at this, maybe it will help.

template<class Tstruct A
{
void func1(int) const;
void func2(int) const;

enum e { one, two, three, four, five };

typedef std::map<e, void (A::*)(int) const funcmap_t;
static funcmap_t mapping;
static int dummy_mapping_i nitialiser;
static int doit() {
mapping.insert( make_pair(one, &A::func1));
mapping.insert( make_pair(two, &A::func2));
}

void call_it(e ee, int i) {
(this->*(mapping[ee]))(i);
}

public:
A() {}
void operate(int);
};

template<class Tstd::map<e, void (A<T>::*)(int) const A<T>::mapping;
template<class Tint A<T>::dummy_map ping_initiliase r = A<T>::doit();

template<class Tvoid A<T>::operate(i nt blah) {
typename funcmap_t::iter ator it = mapping.begin() , e =
mapping.end();
while (it != e) {
call_it(it->first, blah);
++it;
}
}

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 25 '07 #4

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

Similar topics

2
6274
by: Elven | last post by:
Hi! I was trying to find the solution to this problem, but I don't think I could quite come up with the correct keywords to find it, since I'm pretty sure it's been asked before. In short, here's the situation (ignore missing namespaces, etc, since I'm not cut-pasting this..) // a.h template <class B>
3
2122
by: Dave | last post by:
Hello all, I am trying to create a full specialization of a member function template of a class template. I get the following errors: Line 29: 'foo<T1>::bar' : illegal use of explicit template arguments Line 29: 'bar' : unable to match function definition to an existing declaration What am I doing wrong?
9
1965
by: Ian | last post by:
Can it be done? If so, what's the syntax. For example a full specialisation, template <typename T> struct X { template <typename C> void foo( C a ) {} };
4
1529
by: pascal.cathier | last post by:
Is it possible to fully specialize a template member function? If not, why is that so? The following code fails to compile with GCC: class A { public: template < typename T > void foo() {} template <>
5
6352
by: rf | last post by:
As I understand it, it is legal to have a template member function of a non-template class, as in the following example: class XYZ { public: template <class Tvoid fn(T* ptr) { T val = *ptr; cout << val << endl;
2
2064
by: Simon G Best | last post by:
Hello! I'm trying to specialize a member function template of a class template, like this:- template<typename Tclass thingy { public: template<typename UT f (const U &) const; };
16
3951
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
2
1857
by: subramanian100in | last post by:
consider the following program #include <iostream> using namespace std; class Rec { public: Rec(int arg = 10) : val(arg) { }
3
305
by: Alexander Stippler | last post by:
Hey all, what exactly is forbidden/wrong with the following call of a template member function; template <typename T> struct Base { template <int N> void
0
8840
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
9367
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...
1
9131
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6669
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
5981
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
4753
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3189
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
2
2576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2130
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.