Results 1 to 3 of 3

Thread: Addition of Handle::staticCast

  1. #1
    erikdendekker is offline Registered User
    Name: Erik den Dekker
    Organization: TNO
    Project: Sonar processing software
    Join Date
    Mar 2010
    Posts
    3

    Addition of Handle::staticCast

    My suggestion is simple: The smart pointer class used by Ice provides a templated dynamicCast function to up-cast to a smart pointer of a derived object, similarly to up-casting of normal pointer using dynamic_cast in C++. However it provides no staticCast method.

    In some cases, when you already known the actual type pointed to you would use static_cast over dynamic_cast in C++ to avoid the overhead associated with the dynamic cast.

    The implementation of Handle::staticCast is trivial given the existing implementation of Handle::dynamicCast.

  2. #2
    bernard's Avatar
    bernard is offline ZeroC Staff
    Name: Bernard Normier
    Organization: ZeroC, Inc.
    Project: Ice
    Join Date
    Feb 2003
    Location
    Palm Beach Gardens, FL
    Posts
    1,294
    Hi Erik,

    I think a better solution (that you can implement yourself) would be to use a non-member template function such as:

    Code:
    // Warning: untested code!
    
    namespace IceUtil
    {
    
    template<typename T, typename Y> inline Handle<T>
    staticCast(const HandleBase<Y>& p)
    {
        return Handle<T>(static_cast<T*>(p.get()));
    }
    
    }
    and then invoke this staticCast with:
    Code:
      FooPtr x = staticCast<Foo>(bar);
    instead of
    Code:
      FooPtr x = FooPtr::staticCast(bar);
    (perhaps you can update the template to get staticCast<FooPtr>(bar))

    We didn't use this syntax in Ice because older C++ compilers don't support well the function<type>(...) syntax.

    Best regards,
    Bernard
    Bernard Normier
    ZeroC, Inc.

  3. #3
    erikdendekker is offline Registered User
    Name: Erik den Dekker
    Organization: TNO
    Project: Sonar processing software
    Join Date
    Mar 2010
    Posts
    3
    Hi Bernard,

    I tried out your suggestion, but stumbled upon a little corner of C++ that was unknown to me before... It seems that it is not possible to use static_cast to cast an instance of a base class to an instance of a derived class when you use virtual inheritance instead of normal inheritance (And Ice uses virtual inheritance a lot)...

    So, my original suggestion seems to have a very limited use case.

    Erik

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. possible file-handle leaks?
    By dhogan in forum Bug Reports
    Replies: 2
    Last Post: 03-05-2009, 03:53 PM
  2. Replies: 5
    Last Post: 11-05-2007, 11:35 AM
  3. IceInternal::Handle
    By RyanFogarty in forum Help Center
    Replies: 7
    Last Post: 12-18-2004, 12:27 AM
  4. How to handle distributed transactions
    By robert in forum Help Center
    Replies: 3
    Last Post: 10-31-2004, 09:35 PM
  5. Exception handle of ice php
    By fengxb in forum Help Center
    Replies: 2
    Last Post: 04-08-2004, 09:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •