Actions

icon Post
text/html Subscribe
text/html Unsubscribe

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cxx-abi-dev] trivial __dynamic_cast fails?


  • To: David Baraff <deb@xxxxxxxxx>
  • Subject: Re: [cxx-abi-dev] trivial __dynamic_cast fails?
  • From: Daniel Wallin <dalwan01@xxxxxxxxxxxxxx>
  • Date: Mon, 01 Mar 2004 19:33:00 +0100

David Baraff wrote:
David Baraff <deb@xxxxxxxxx> writes:


David Abrahams wrote:

<snip>

Incidentally, my system stores those offsets as an optimization for
all platforms that can handle it (which so far is everything) but if
you want to be strict about portable conformance you have to walk
through the cast graph.

I do, so I end up calling the synthesized cast functions several times in a single cast.

Maybe irrelevant but...

If you disregard strict conformance or are just interested in casting
up, it is possible to use the exception trick Dave A demonstrated in the
modified boost::any implementation to make the macro usage a little less
verbose and a bit more robust by omitting the bases.

  template<class T>
  T* cast_up(void* p, std::type_info const& type)
  {
      try
      {
          // will throw static_cast<U*>(p), where type = typeid(U)
          throw_registered_pointer(p, type);
      }
      catch (T* converted)
      {
          return converted;
      }
      catch (...)
      {
          return 0;
      }
  }

  // Now we just need to register the throw functions:
  struct A {};
  REGISTER_CLASS(A);

  struct B : A {};
  REGISTER_CLASS(B);

It should be possible to use the same trick to generate a cast graph
with byte offsets in the edges, if strict conformance isn't important.

--
Daniel Wallin