Please take a look at this method:
template<class C> void f(C* ptrAny) {
Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny);
if(ptrFruit) {
// do something specific to fruits
}
// Carry on using ptrAny whatever it is
}
So I have a template function, but for Fruits I also want to do a
certain specific thing.
All is well and good until I happen to call F with a non-polymorphic
type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or
not? NB I don't want to overload f with a non-template like this:
void f(Fruit* ptrFruit) {...}
....because I have several templated functions like f, and also because
how would the compiler know which one to call for a Fruit, the
Fruit-specific one or the templated one instantiated for a Fruit?
Thanks for any help. 10 3653 ri***********@yahoo.co.uk wrote: Please take a look at this method:
template<class C> void f(C* ptrAny) { Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny); if(ptrFruit) { // do something specific to fruits } // Carry on using ptrAny whatever it is }
So I have a template function, but for Fruits I also want to do a certain specific thing. All is well and good until I happen to call F with a non-polymorphic type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or not? NB I don't want to overload f with a non-template like this: void f(Fruit* ptrFruit) {...}
I think you have no choice. "Specialization" is the name of the game.
...because I have several templated functions like f
so what?
, and also because how would the compiler know which one to call for a Fruit, the Fruit-specific one or the templated one instantiated for a Fruit?
the compiler is smart enough to pick the specialized function instead
of the templated one Thanks for any help.
see the code below:
class Fruit
{
public:
void IsRipe(){}
virtual ~Fruit(){}
};
class NonF
{
};
template <class C>
void f_impl(C* ptrAny)
{
// do the common stuff
}
template<class C>
void f(C* ptrAny)
{
// do the common processing
f_impl(ptrAny);
}
template<>
void f(Fruit* ptrFruit)
{
// do something specific for fruit
ptrFruit->IsRipe();
// then, do the common processing
f_impl(ptrFruit);
}
int main()
{
Fruit fr;
NonF nf;
f(&fr);
f(&nf);
return 0;
}
/dan ri***********@yahoo.co.uk sade: void f(Fruit* ptrFruit) {...} ...because I have several templated functions like f, and also because how would the compiler know which one to call for a Fruit, the Fruit-specific one or the templated one instantiated for a Fruit?
Then perhaps a template specialization, no compiler confusion there.
template<typename T> void f(T *) {}
template<> void f(Fruit *) {}
Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
<ri***********@yahoo.co.uk> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com... Please take a look at this method:
template<class C> void f(C* ptrAny) { Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny); if(ptrFruit) { // do something specific to fruits } // Carry on using ptrAny whatever it is }
So I have a template function, but for Fruits I also want to do a certain specific thing. All is well and good until I happen to call F with a non-polymorphic type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or not? NB I don't want to overload f with a non-template like this: void f(Fruit* ptrFruit) {...} ...because I have several templated functions like f, and also because how would the compiler know which one to call for a Fruit, the Fruit-specific one or the templated one instantiated for a Fruit?
Thanks for any help.
Why don't you do:
void f(Fruit* ptrFruit)
{
FruitSpecific();
f<Fruit>();
}
template <typename T>
void f(T* ptrAny)
{
// carry on
}
Ben
Dan Cernat skrev: ri***********@yahoo.co.uk wrote: Please take a look at this method:
template<class C> void f(C* ptrAny) { Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny); if(ptrFruit) { // do something specific to fruits } // Carry on using ptrAny whatever it is }
So I have a template function, but for Fruits I also want to do a certain specific thing. All is well and good until I happen to call F with a non-polymorphic type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or not? NB I don't want to overload f with a non-template like this: void f(Fruit* ptrFruit) {...} I think you have no choice. "Specialization" is the name of the game.
...because I have several templated functions like f so what?
Well, that is the problem. At least if you assume the OP's dynamic_cast
made any sense at all. , and also because how would the compiler know which one to call for a Fruit, the Fruit-specific one or the templated one instantiated for a Fruit? the compiler is smart enough to pick the specialized function instead of the templated one
Thanks for any help.
see the code below:
Your code is wrong - see below. class Fruit { public: void IsRipe(){} virtual ~Fruit(){} };
class NonF { };
template <class C> void f_impl(C* ptrAny) { // do the common stuff }
template<class C> void f(C* ptrAny) { // do the common processing f_impl(ptrAny); }
template<> void f(Fruit* ptrFruit) { // do something specific for fruit ptrFruit->IsRipe();
// then, do the common processing f_impl(ptrFruit); }
int main() { Fruit fr; NonF nf;
f(&fr); f(&nf);
return 0; }
The problem is that you suppose that fruit is the base class, but it
can not be as there would then be no reason to use a dynamic cast.
Change you program as follows:
class Edible
{
virtual ~Edible();
};
class Fruit: Edible;
....
Edible *e = new Fruit();
f(e);
/dan
/Peter ri***********@yahoo.co.uk skrev: Please take a look at this method:
template<class C> void f(C* ptrAny) { Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny); if(ptrFruit) { // do something specific to fruits } // Carry on using ptrAny whatever it is }
So I have a template function, but for Fruits I also want to do a certain specific thing. All is well and good until I happen to call F with a non-polymorphic type. Then it barfs with a compile error.
You need to use template metaprogramming if indeed that is what you
want. Look at boost for the needed tools (the library is called mpl). So how can I do this where the templated C type can be polymorphic or not? NB I don't want to overload f with a non-template like this: void f(Fruit* ptrFruit) {...} ...because I have several templated functions like f, and also because how would the compiler know which one to call for a Fruit, the Fruit-specific one or the templated one instantiated for a Fruit?
If Fruit is a base-class (e.g. you intend to call it with a class
Banana: public Fruit), then the nontemplated function is the way to go.
That presumes that your dynamic_cast was wrong. If it is the other way
around (e.g. class Fruit::public Edible) you should either create a
function void f(Edible* e) and direct your non-fruit calls to the
generic class or take the templateroute as you seem to be determined to
use. I just do not see how templates give you anything here. Thanks for any help.
/Peter
"Tobias Blomkvist" <vo**@void.void> wrote in message
news:1122384411.d43190a858164d921f3d8bfb2904db16@t eranews... ri***********@yahoo.co.uk sade: void f(Fruit* ptrFruit) {...} ...because I have several templated functions like f, and also because how would the compiler know which one to call for a Fruit, the Fruit-specific one or the templated one instantiated for a Fruit?
Then perhaps a template specialization, no compiler confusion there.
template<typename T> void f(T *) {}
template<> void f(Fruit *) {}
Tobias
-- IMPORTANT: The contents of this email and attachments are confidential and may be subject to legal privilege and/or protected by copyright. Copying or communicating any part of it to others is prohibited and may be unlawful.
Is it a new standard that function templates can be specialized? What is
template<> void f(Fruit*){}
supposed to differ from
void f(Fruit*){}
??
Ben
"Tobias Blomkvist" <vo**@void.void> wrote in message
news:1122384411.d43190a858164d921f3d8bfb2904db16@t eranews... ri***********@yahoo.co.uk sade: void f(Fruit* ptrFruit) {...} ...because I have several templated functions like f, and also because how would the compiler know which one to call for a Fruit, the Fruit-specific one or the templated one instantiated for a Fruit?
Then perhaps a template specialization, no compiler confusion there.
template<typename T> void f(T *) {}
template<> void f(Fruit *) {}
Tobias
-- IMPORTANT: The contents of this email and attachments are confidential and may be subject to legal privilege and/or protected by copyright. Copying or communicating any part of it to others is prohibited and may be unlawful.
Is it a new standard that function templates can be specialized? What is
template<> void f(Fruit*){}
supposed to differ from
void f(Fruit*){}
??
Ben
benben sade: Is it a new standard that function templates can be specialized? What is
C++ Standard §14.7 template<> void f(Fruit*){}
supposed to differ from
void f(Fruit*){}
??
Ben
My reply wasn't the best in that context, I admit.
But you could separate them:
Fruit * g = new Fruit;
f<Fruit>(g); // calls template specialization
Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
> C++ Standard §14.7
My reply wasn't the best in that context, I admit.
But you could separate them:
Fruit * g = new Fruit; f<Fruit>(g); // calls template specialization
Tobias
It seems to be a fairly new standard because I couldn't find it in both
Bjarne's and Nicolai's books (or I just missed that part.) But I still don't
have an idea how is function template specialization different from simple
function overloading. It seems to me they are really the same but I doubt.
Any hint would be much appreciated!
Ben
benben sade: It seems to be a fairly new standard because I couldn't find it in both Bjarne's and Nicolai's books (or I just missed that part.) But I still don't
Depends on if you call the first C++ standard ISO/IEC 14882:1998
from 1998 new or not. If you don't have it, you could
always scout the final draft on templates, available all over
the internet: http://www.kuzbass.ru:8086/docs/isocpp/template.html
Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Surya Kiran |
last post by:
Hi all,
I've written a function template. say
template <class T>
fn (T var) { ... }
Is there any way, from within the function, can we check what type of
argument we've passed on to the...
|
by: iuweriur |
last post by:
A few questions on the curiously recurring template pattern:
This page:
http://c2.com/cgi/wiki?CuriouslyRecurringTemplate
this part:
template<typename T> struct ArithmeticType
{
T operator...
|
by: Simon Elliott |
last post by:
Given a template function:
template <class T>
void foo(T& value)
{
// do something with value
}
Is there any way I can tell whether the type T is an STL container?
|
by: Imre |
last post by:
Why is the function template a better match for the call in the
following code?
And how could I write a version of F() that should be called if the
argument is a pointer to a type that is...
|
by: Steven T. Hatton |
last post by:
Perhaps I'm just a bit frustrated, and I will soon realize the clear truth
of the matter, but right now I have some serious misgivings about the value
of investing a lot of time and effort into...
|
by: chris.kemmerer |
last post by:
I am having a problem with templates and I hope someone here can help.
I am writing a library that accepts data packets, parses them and
saves the information for later use. One member of the...
|
by: mathieu |
last post by:
Hi there,
I don't think I'll be able to describe my issue correctly, so
instead I'll just give a pseudo C++ code I am struggling with.
Basically I am looking for a 'pure virtual template'...
|
by: jfradley |
last post by:
Hello,
Here's my problem. I have a template function where I'd like to set a flag if the passed in data type is derived from a particular base class. The way to typically do this is to use...
|
by: Markus Dehmann |
last post by:
I have an abstract base class which contains a function that I would
like to template, but virtual template functions are illegal. I put a
mini code example below, which doesn't do anything great,...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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...
|
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...
|
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: 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,...
| |