473,473 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to specialized the template function when its parameter type are same?

2 New Member
template <class A, class B> inline B* mycast( A* a)
{
return dynamic_cast<B>(a);
}

// try to specialize the mycast to handle when B==A, such as:
template <class A> inline A* mycast(A* a)
{
return a; // since type B is identical to A.
}
// OR what else I should do to implement this function??????

//....somewhere we try to use mycast()
test()
{
A* ap;
B* bp = mycast(ap); // I expect this will call the primary template function.

A* ap2 = mycast(ap); // I expect this will call the specialized tuned template function.
}

// unfortunately, above code is not run as I expected.

As a "derived question" from above, is the "dynamics_cast<A>(a)" EXACTLY(performance??) idential to "A(a)" when a is type of "A"???
Oct 10 '07 #1
2 1301
RRick
463 Recognized Expert Contributor
The second template definition is not a specialization of the first, rather it is a new (and different) definition.

Another problem you're going to run into is that both of your templated functions differ only by the return value. When the compiler see these two definitions, it will not use the return value in its comparison. In this case, it won't know which one to pick.

You can get around this ambiguity by explicitly defining which mycast function you want used, but this probably defeats the purpose of what you're trying to do. For example,
Expand|Select|Wrap|Line Numbers
  1. Derive* dPtr = mycast<Base,Derive>( bPtr);
  2. Base* bPtr2 = mycast<Base>( bPtr);
Oct 11 '07 #2
skydoom
2 New Member
I had fingered out what I should do now :)

Indeed, I initially want some 'partial template specialization' for the template function, which is not currently supported in the C++ standard(which _might_ be in the next C++0x). But I do find some workaround for that:

// declare a template class with a friend template function.
template<typename B, typename D>
class mycast{
friend D* fun_cast<B,D>(B* b);
public:
D* operator ()(B* b)
{
return dynamic_cast<D*>(b);
}
private:
~mycast(){}
};

// !!!! partial specialization for the "SAME TYPE" casting.
template<typename D>
class mycast<D,D>{
friend D* fun_cast<D,D>(D* b);
public:
D* operator ()(D* b)
{
return b;
}
private:
~mycast(){}
};

// This is the interface to the user, the template function below will use the correct one.
template < typename B, typename D>
D* fun_cast(B* b)
{
return mycast<B,D>()(b);
}

Thanks anyone.

PS. As the last question, how's the performance for dynamics_cast<A*>(a), while a is indeed of type A? (is it identical to the A(a) by the compiler?)
Oct 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
16
by: PeterAPIIT | last post by:
Hello all C++ expert programmer, i have wrote partial general allocator for my container. After reading standard C++ library and code guru article, i have several questions. 1. Why...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
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...
1
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...
1
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.