473,378 Members | 1,400 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,378 software developers and data experts.

template specialization

Does anyone know if you can specialize a template function for
arguments that are pointers to a particular base class and not lose
the subclass type?

example:

template <class E>
void DeliverEvent(E *Event);

is a function that will receive an Event object, and then eventually
pass it to the proper destination. To avoid downcasting, any
destination will have a "ReceiveEvent" function that is overloaded for
each specific Event class it is interested in.

There is a particular class of Events, call them "CallEvents", which
require special processing before passing them along. So I want to
specialize the function as such:

template <>
void DeliverEvent(CircuitEvent *Event);

But in doing it this way, the Event object becomes a base class
pointer, and I lose its specific type (and I would like to avoid
downcasting to regain it).

Is there a way to specialize the template function so that my template
argument keeps it type?

Thanks,
Eric Simon
Jul 22 '05 #1
4 4718
Hey Eric, I must be missing something here, hope I don't make a total
fool of myself ...
template <class E>
void DeliverEvent(E *Event);
template <>
void DeliverEvent(CircuitEvent *Event);


Don't you need to specify the type in the specialization (hope I've
got this right...) ?
template <>
void DeliverEvent<CircuitEvent>(CircuitEvent* Event);
------------

Second, if you're dealing with a class heirarchy, why do you need to
use templates here at all? Can't you just overload the function?
(pardon the silly question)
void DeliverEvent(Event* e);
void DeliverEvent(CircuitEvent* e);

Jeff
Jul 22 '05 #2
On 20 Nov 2003 15:32:44 -0800, pu*****@hotmail.com (Eric) wrote:
Does anyone know if you can specialize a template function for
arguments that are pointers to a particular base class and not lose
the subclass type?

example:

template <class E>
void DeliverEvent(E *Event);

is a function that will receive an Event object, and then eventually
pass it to the proper destination. To avoid downcasting, any
destination will have a "ReceiveEvent" function that is overloaded for
each specific Event class it is interested in.

There is a particular class of Events, call them "CallEvents", which
require special processing before passing them along. So I want to
specialize the function as such:

template <>
void DeliverEvent(CircuitEvent *Event);

But in doing it this way, the Event object becomes a base class
pointer, and I lose its specific type (and I would like to avoid
downcasting to regain it).

Is there a way to specialize the template function so that my template
argument keeps it type?
Yes, but it's a bit complicated (first download boost from
www.boost.org).

Basically you don't specialize the template, but instead dispatch to a
particular implementation based on properties of the passed type. In
this case, your looking for SpecialEvent and classes derived from it.

#include <iostream>
#include <boost/type_traits.hpp>
using namespace boost;

struct Event
{
};

struct SpecialEvent
{
virtual ~SpecialEvent() {}

void non_virtual()
{
std::cout << "SpecialEvent::non_virtual\n";
}
};

struct DerivedSpecialEvent: SpecialEvent
{
void non_virtual()
{
std::cout << "DerivedSpecialEvent::non_virtual\n";
}
};

template <bool>
struct DeliverEventImpl
{
template <class Event>
static void do_it(Event* event)
{
std::cout << "Non specialized\n";
}
};

template<>
struct DeliverEventImpl<true>
{
//SpecialEvent version
template <class Event>
static void do_it(Event* event)
{
event->non_virtual();
}
};
template <class E>
void DeliverEvent(E* event)
{
DeliverEventImpl<
is_base_and_derived<SpecialEvent, E>::value
|| is_same<SpecialEvent, E>::value::do_it(event);

}

int main()
{
Event e;
SpecialEvent se;
DerivedSpecialEvent dse;
DeliverEvent(&e);
DeliverEvent(&se);
DeliverEvent(&dse);
}

Tom
Jul 22 '05 #3
je********@yahoo.com (Jeff) wrote in message news:<7b**************************@posting.google. com>...
Hey Eric, I must be missing something here, hope I don't make a total
fool of myself ...
template <class E>
void DeliverEvent(E *Event);
template <>
void DeliverEvent(CircuitEvent *Event);
Don't you need to specify the type in the specialization (hope I've
got this right...) ?
template <>
void DeliverEvent<CircuitEvent>(CircuitEvent* Event);


Yes, you are right. My mistake.
------------

Second, if you're dealing with a class heirarchy, why do you need to
use templates here at all? Can't you just overload the function?
(pardon the silly question)
void DeliverEvent(Event* e);
void DeliverEvent(CircuitEvent* e);
The problem is that Event and Circuit Event are base classes. There
are several concrete classes that inherit from Event. Circuit Event
also inherits from Event, and in turn, has several concrete classes
inheriting from it. I use templates because I don't want to have to
downcast to regain the subclass type. I guess it's a very weak
hierarchy - the base class has very little functionality. Most of the
interesting stuff is specific to each subclass.

Thanks,
Eric

Jeff

Jul 22 '05 #4
> > Second, if you're dealing with a class heirarchy, why do you need to
use templates here at all? Can't you just overload the function?
(pardon the silly question)
void DeliverEvent(Event* e);
void DeliverEvent(CircuitEvent* e);


The problem is that Event and Circuit Event are base classes. There
are several concrete classes that inherit from Event. Circuit Event
also inherits from Event, and in turn, has several concrete classes
inheriting from it. I use templates because I don't want to have to
downcast to regain the subclass type.


I'm still puzzled as to why you need templates, though ... shouldn't
polymorphism do the trick? In Scott Meyers's "Effective C++, 2nd ed",
Item 41 says "Differentiate between inheritance and templates" -- the
section is aimed at class templates, but can be generalized to
functions as well. The summary of the item, copied from the book, is
here:

- A template should be used to generate a collection of classes when
the type of the objects /*does not*/ affect the behaviour of the
class's functions.

- Inheritance should be used for a collection of classes when the type
of the objects /*does*/ affect the behaviour of the class's functions.

Since you're talking about changing the behaviour of a particular
function depending on the type of item passed to it, polymorphism (or,
in this case, function overloading) seems to be called for...

But perhaps I'm missing something. Can you boil your problem down to
a simple representation and post it? That way I (and others) might be
able to comment more appropriately on your problem (perhaps creating a
new thread in this group, and copying the messages from this thread to
the new post).

Jeff
Jul 22 '05 #5

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
2
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
2
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem...
6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit...
4
by: TT \(Tom Tempelaere\) | last post by:
Comeau compiler complains (too few arguments for class template "B") at line *** #include <memory> template<typename T, size_t n> struct A {}; template<typename T, size_t n> struct B;
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
by: Thomas Kowalski | last post by:
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:...
2
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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...

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.