 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthias Hofmann Guest
|
Posted: Sat Aug 16, 2003 1:43 pm Post subject: Overloading template parameters |
|
|
Hello!
I am trying to write a template function that performs a dynamic_cast and
performs an error check. As the error checking is different for pointers and
references, I am trying to overload the template functions. Here's the code,
which does not work (at least on VC++ 6.0) but should clarify what I am
trying to achieve:
// Template function for pointers.
template <class D, class S> D* AssertCast( S* ps )
{
D* pd = dynamic_cast<D*>( ps );
if ( pd != NULL ) { /* Handle error. */}
return pd;
}
// Template function for references.
template <class D, class S> D& AssertCast( S& rs )
{
try { return dynamic_cast<D&>( rs ); }
catch ( bad_cast& ) { /* Handle error. */ }
}
class A {};
class B : public A {};
int main()
{
B* pb = new B;
A* pa = AssertCast<A*>( pb ); // Should call pointer version.
A& ra = AssertCast<A&>( *pb ); // Should call reference version.
delete pb;
return 0;
}
I would like the compiler to invoke the template function for pointers in
the first case and the template function for references in the second case.
However, VC++6.0 rejects the code. I don't know if this is one of the usuall
template problems on this compiler or wether I oversaw anything.
Please help.
Best regards,
Matthias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matt Messina Guest
|
Posted: Wed Sep 17, 2003 10:16 am Post subject: Re: Overloading template parameters |
|
|
On 16 Aug 2003, Matthias Hofmann <hofmann (AT) anvil-soft (DOT) com> wrote:
| Quote: | I am trying to write a template function that performs a dynamic_cast and
performs an error check. As the error checking is different for pointers and
references, I am trying to overload the template functions. Here's the code,
which does not work (at least on VC++ 6.0) but should clarify what I am
trying to achieve:
|
| Quote: | // Template function for pointers.
template <class D, class S> D* AssertCast( S* ps )
|
If D is explictly specified as A*, then the return type would be A**.
That's not what you want.
| Quote: | class A {};
class B : public A {};
|
Note that dynamic_cast requires a polymorphic type.
| Quote: | A* pa = AssertCast<A*>( pb ); // Should call pointer version.
A& ra = AssertCast<A&>( *pb ); // Should call reference version.
I would like the compiler to invoke the template function for pointers in
the first case and the template function for references in the second case.
However, VC++6.0 rejects the code. I don't know if this is one of the usuall
template problems on this compiler or wether I oversaw anything.
|
Here is a solution using Loki to combine overloading and explicit
function template arguments.
#include <cstddef>
#include <typeinfo>
#include "Loki/TypeManip.h"
#include "Loki/TypeTraits.h"
using std::bad_cast;
using Loki::Int2Type;
using Loki::TypeTraits;
template<class D, class S> D AssertCastAux(S* ps, Int2Type<false>)
{
D pd = dynamic_cast<D>(ps);
if (pd == NULL) { /* Handle error. */ }
return pd;
}
template<class D, class S> D AssertCastAux(S& rs, Int2Type<true>)
{
try { return dynamic_cast<D>(rs); }
catch (bad_cast&) { /* Handle error. */ }
}
template<class D, class S> D AssertCast(S& s)
{
return AssertCastAux<D>(s, Int2Type<TypeTraits());
}
struct A { virtual ~A() { } };
struct B : public A { };
struct C : public A { };
int main()
{
A* pa = new B;
B* pb = AssertCast<B*>(pa);
B& rb = AssertCast<B&>(*pa);
/*
C* pc = AssertCast<C*>(pa);
C& rc = AssertCast<C&>(*pa);
*/
delete pa;
}
I don't have access to MSVC, but Andrei's web site
<http://www.moderncppdesign.com/> references "the most complete port of
Loki to Microsoft Visual C++ 6".
You can use Boost rather than Loki if you prefer:
"Loki/TypeManip.h" => "boost/mpl/bool.hpp"
"Loki/TypeTraits.h" => "boost/type_traits.hpp"
Loki::Int2Type => boost::mpl::bool_
Loki::TypeTraits<>::isReference => boost::is_reference<>::value
Or you could roll your own Int2Type and isReference.
--
Matt Messina
[email]messina (AT) yahoo (DOT) com[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
White Wolf Guest
|
Posted: Wed Sep 17, 2003 8:33 pm Post subject: Re: Overloading template parameters |
|
|
Matt Messina wrote:
| Quote: | class A {};
class B : public A {};
Note that dynamic_cast requires a polymorphic type.
|
Not always. See 5.2.7 in the standard.
--
WW aka Attila
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|