473,396 Members | 1,982 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.

Template spezialization

Hi,
I would like to write a template class Polygon<VertexTypthere vertex
typ can be eigther a pointer or a value typ.

It has an attribute:
std::vector<VertexTypvertices;

And a methode:
VertexValueTyp& vertex(int i);
that dereferences vertices[i] internally in case VertexTyp is a pointer
type.

How can I achieve this? Are there any step-by-step guides online?

Thanks in advance,
Thomas Kowalski

Sep 24 '06 #1
2 2467
Thomas Kowalski <th***@gmx.dewrote:
I would like to write a template class Polygon<VertexTypthere vertex
typ can be eigther a pointer or a value typ.

It has an attribute:
std::vector<VertexTypvertices;

And a methode:
VertexValueTyp& vertex(int i);
that dereferences vertices[i] internally in case VertexTyp is a
pointer type.

How can I achieve this? Are there any step-by-step guides online?
The following template and its specialization should give you an
idea:

template <typename T>
class A
{

};

template <typename S>
class A <S*>
{
};

int main ()
{
A <charaa;
A <char*ab;
}

You would have to completely reimplement the specialization of the
template class.

There are also ways to move the specialization out of the main class
by using inheritance:

template <typename T>
class special_A
{
// stuff for non-pointers
};

template <typename S>
class special_A <S*>
{

// stuff for pointers
};

template <typename T>
class A : special_A <T>
{
// common functions etc which will be merged
// with the special things
};

int main ()
{
A <charaa;
A <char*ab;
}

Now you can provide different implementations in the 'special_A'
template and its specialization and the common stuff in the template
class 'A'. One problem you will face is the ability to cast 'A <char>'
to 'special_A <char>' if you use public inheritance (so your special
functions are exposed). But if you use private inheritance, your special
functions are not available to the user of 'A <char>'. One solution
could be to create a typedef inside the special_A template and its
specialization, which determine the return type of the common function
and use private inheritance:

#include <cstddef>

template <typename T>
class special_A
{
protected:
typedef T ret_t;

ret_t impl ();
};

template <typename S>
class special_A <S*>
{
protected:
typedef S ret_t;

ret_t impl ();
};

template <typename T>
class A : special_A <T>
{
public:
typename special_A <T>::ret_t get_element (std::size_t idx) { return
special_A <T>::impl (); }
};

int main ()
{
A <charaa;
A <char*ab;

char ret_aa = aa.get_element (0);
char ret_ab = ab.get_element (0);
}

Now you cannot legally cast the 'A <char>' objects to 'special_A
<char>' and you can still reuse common functions of your class without
having to rewrite them for the specialization.

hth
--
jb

(reply address in rot13, unscramble first)
Sep 24 '06 #2

Thomas Kowalski wrote:
Hi,
I would like to write a template class Polygon<VertexTypthere vertex
typ can be eigther a pointer or a value typ.

It has an attribute:
std::vector<VertexTypvertices;

And a methode:
VertexValueTyp& vertex(int i);
that dereferences vertices[i] internally in case VertexTyp is a pointer
type.

How can I achieve this? Are there any step-by-step guides online?

Thanks in advance,
Thomas Kowalski
You have not given any details about VertexTyp. We don't know if you
are dealing with a template parameter that has member functions or is
it just a POD. I'm going to assume a POD for now.

As far as the method is concerned, its actually an operator that you
will prefer dealing with ( operator[] ). Basicly, since Polygon is just
a container wrapping a std::vector, why not match the interface that
std::vector already provides?

Here is an example that uses the name Container instead of Polygon and
a simple int instead of VertexTyp. Lets face it, if you can't get a
templated container handling primitive integers and their pointers
working, how will u be able to get to work with a more complex type?

#include <iostream>
#include <ostream>
#include <vector>

template < typename T >
class Container
{
std::vector< T v;
public:
Container() : v() { }
Container(size_t sz) : v(sz) { }
~Container() { }
/* member functions */
size_t size() const { return v.size(); }
void push_back( const T& r_t ) { v.push_back(r_t); }
T& operator[]( const unsigned index ) { return v.at(index); }
};

int main()
{
const unsigned sz( 5 );
Container< int integers(sz); // a vector of 5 integers
Container< int* pointers; // an empty vector of pointers to int

for ( size_t i = 0; i < integers.size(); ++i )
{
integers[i] = static_cast<int>(i);
pointers.push_back( &integers[ i ] );
}

// test
*pointers[0] = 101;

for ( size_t i = 0; i < integers.size(); ++i )
{
std::cout << pointers[i] << "\t";
std::cout << integers[i] << std::endl;
}
return 0;
}

/*
0x503010 101
0x503014 1
0x503018 2
0x50301c 3
0x503020 4
*/

Remember that your VertexTyp needs to support copy semantics.

Sep 24 '06 #3

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

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
31
by: nikola | last post by:
Hi all, I was working with a simple function template to find the min of two values. But since I would like the two values to be different (type) I dont know what kind of value (type) it will...
5
by: Gianni Mariani | last post by:
The spirit of this arguably pointless exercise, is that the numeric_limits<T> class could be replaced with a totally generic template of compile-time, template computed constants. The problem is...
2
by: Markus Mauhart | last post by:
Hi, I get an unexpected complaint from the compiler: -------------------------- enum E {e0,e1}; template <class tx ,E ty> struct A; template <class tx> struct A <tx,e1> {};//OK
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
2
by: Alfonso Morra | last post by:
I have a class declared as ff: class __declspec(dllexport) A { public: A() ; A(const A&) A& operator=(const A&) ; ~A() ; void doThis(void) ;
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
45
by: charles.lobo | last post by:
Hi, I have recently begun using templates in C++ and have found it to be quite useful. However, hearing stories of code bloat and assorted problems I decided to write a couple of small programs...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.