Ronald Garcia wrote:
>
> hey guys,
> got a question about some code I found in orbitcpp_tools.hh.
>
> There is some code which goes like so:
>
> #ifdef ORBITCPP_HAVE_BAD_CAST
> #define ORBITCPP_NARROW_OR_NULL(type,item) \
> type *result; \
> try { \
> result = dynamic_cast<type *>(item); \
> } \
> catch (bad_cast) {\
> result = NULL; \
> }
> #else
> #warning ORBit-C++ will not detect narrowing errors on pseudo objects.
> #define ORBITCPP_NARROW_OR_NULL(type,item) \
> type *result = static_cast<type *>(item);
> #endif
>
> a few questions/comments:
>
> 1) In C++, dynamic casting to a pointer type doesn't throw
> exceptions, it just returns a 1. bad_cast is thrown on dynamic casts
> to references...ie, I don't think that code will work.
whoops. you're right.
>
> 2) I looked at the code to set ORBITCPP_HAVE_BAD_CAST in
> acinclude.m4... as far as I recall, in g++, bad_cast can be found in
> <typeinfo> instead of <exception>. I think this is the wrong place
> according to c++ standards, but if we're going to use it, we might as
> well support the most common compiler for this stuff.
if we decide to keep the code, we should probably add g++ compatibility
and fix it.
> 3) I think that the above could be implemented as a template,
> n'est-ce pas? That would be the c++ way
true. it's just those damn old habits, you know :))
bye
andy
|