Ambiguity with Smart Pointers (Boost or similar)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • number774@netscape.net

    Ambiguity with Smart Pointers (Boost or similar)

    I've used Boost for this example; in fact we have our own pointer
    class, for historic reasons.

    #include "boost\shared_p tr.hpp"

    // A heirarchy of classes
    class G1 {};
    class G2: public G1 {};

    // A method which takes a shared pointer to the base class
    void f(boost::shared _ptr<G1>) {};

    // A method which takes a shared pointer to some other class
    void f(boost::shared _ptr<int>) {};

    void main()
    {
    // Call a method with a shared pointer to a derived class
    f(boost::shared _ptr<G2>());
    }

    This fails, because the compiler is unable to decide which of the two
    methods is meant. Of course, to a human, it's pretty obvious which
    one is meant, but there doesn't seem to be enough information at the
    time for the compiler to decide. In practice I have several hundred
    functions, and 50 or so classes, to get confused among, so I don't
    really want to take the obvious route [declare an
    f(boost::shared _ptr<G2>) ]. Is there anything I can do to the pointer
    class, or even to the class inheritance, that'll help the compiler
    out?

    Thx
  • kwikius

    #2
    Re: Ambiguity with Smart Pointers (Boost or similar)

    On 3 Jan, 12:11, number...@netsc ape.net wrote:
    I've used Boost for this example;  in fact we have our own pointer
    class, for historic reasons.
    >
    #include "boost\shared_p tr.hpp"
    >
    // A heirarchy of classes
    class G1 {};
    class G2: public G1 {};
    >
    // A method which takes a shared pointer to the base class
    void f(boost::shared _ptr<G1>) {};
    >
    // A method which takes a shared pointer to some other class
    void f(boost::shared _ptr<int>) {};
    >
    void main()
    {
            // Call a method with a shared pointer to a derived class
            f(boost::shared _ptr<G2>());
    >
    }
    >
    This fails, because the compiler is unable to decide which of the two
    methods is meant.  Of course, to a human, it's pretty obvious which
    one is meant, but there doesn't seem to be enough information at the
    time for the compiler to decide.  In practice I have several hundred
    functions, and 50 or so classes, to get confused among, so I don't
    really want to take the obvious route [declare an
    f(boost::shared _ptr<G2>) ].  Is there anything I can do to the pointer
    class, or even to the class inheritance, that'll help the compiler
    out?
    >
    Thx
    I don't use boost::shared_p tr, but I am surprised if it cant
    discriminate the above case, though maybe this is the result of an old
    compiler, which may mean it cant use some facilities.

    Hint! use some SFINAE, in combination with std::tr1 type_traits, e.g
    is_base_of etc, but as I said a good smart pointer should easily
    handle this case automatically.

    regards
    Andy Little

    Comment

    • number774@netscape.net

      #3
      Re: Ambiguity with Smart Pointers (Boost or similar)

      On Jan 3, 12:57 pm, kwikius <a...@servocomm .freeserve.co.u kwrote:
      >
      I don't use boost::shared_p tr, but I am surprised if it cant
      discriminate the above case, though maybe this is the result of an old
      compiler, which may mean it cant use some facilities.
      >
      Fails with MS VC 2005 AND Gcc 4.01 on an Apple...
      Hint! use some SFINAE, in combination with std::tr1 type_traits, e.g
      is_base_of etc, but as I said a good smart pointer should easily
      handle this case automatically.
      If you know a better smart pointer, please let me know :) .
      Meanwhile, I'll keep playing. is_base_of isn't in my copy of the
      standard [2003, 2nd ed] but there are web refs to it in Boost which
      I'll read.

      Thanks

      Comment

      • kwikius

        #4
        Re: Ambiguity with Smart Pointers (Boost or similar)

        On 3 Jan, 14:17, kwikius <a...@servocomm .freeserve.co.u kwrote:


              typename quanta::where_<
                 std::tr1::is_ba se_of<T,Derived >,
                 void*
              >::type* =0
        ...Oops try changing quanta::where to enable_if ;-)

        regards
        Andy Little

        Comment

        • Salt_Peter

          #5
          Re: Ambiguity with Smart Pointers (Boost or similar)

          On Jan 3, 7:11 am, number...@netsc ape.net wrote:
          I've used Boost for this example; in fact we have our own pointer
          class, for historic reasons.
          >
          #include "boost\shared_p tr.hpp"
          >
          // A heirarchy of classes
          class G1 {};
          class G2: public G1 {};
          >
          // A method which takes a shared pointer to the base class
          void f(boost::shared _ptr<G1>) {};
          >
          // A method which takes a shared pointer to some other class
          void f(boost::shared _ptr<int>) {};
          >
          void main()
          {
          // Call a method with a shared pointer to a derived class
          f(boost::shared _ptr<G2>());
          >
          }
          >
          This fails, because the compiler is unable to decide which of the two
          methods is meant. Of course, to a human, it's pretty obvious which
          one is meant, but there doesn't seem to be enough information at the
          time for the compiler to decide. In practice I have several hundred
          functions, and 50 or so classes, to get confused among, so I don't
          really want to take the obvious route [declare an
          f(boost::shared _ptr<G2>) ]. Is there anything I can do to the pointer
          class, or even to the class inheritance, that'll help the compiler
          out?
          >
          Thx
          How about templates:

          #include <iostream>
          #include "boost/shared_ptr.hpp"

          class G1
          {
          public:
          ~G1() { std::cout << "~G1()\n"; }
          };

          class G2: public G1
          {
          public:
          ~G2() { std::cout << "~G2()\n"; }
          };

          template< typename T >
          void f(boost::shared _ptr< T bsp)
          {
          std::cout << "void f(boost::shared _ptr< T >&)\n";
          }

          template<>
          void f(boost::shared _ptr<G1bsp)
          {
          std::cout << "void f(boost::shared _ptr<G1>&)\n";
          }

          template<>
          void f(boost::shared _ptr<intbsp)
          {
          std::cout << "void f(boost::shared _ptr<int>&)\n";
          }

          int main()
          {
          f(boost::shared _ptr<G1>(new G2));
          }

          /*
          void f(boost::shared _ptr<G1>&)
          ~G2()
          ~G1() // even though that dtor isn't virtual!
          */

          Comment

          • number774@netscape.net

            #6
            Re: Ambiguity with Smart Pointers (Boost or similar)

            On Jan 3, 4:27 pm, Salt_Peter <pj_h...@yahoo. comwrote:
            <snip>
            int main()
            {
            f(boost::shared _ptr<G1>(new G2));
            }
            ^^^^^^^^^^^^^^^ ^^^^^^
            That's the critical bit. (or at least, if you are in the same font as
            me :) ) If the pointer passed to f *is* a shared_ptr<G1th en there's
            no confusion. It's only when it's some other type that the compiler
            goes hunting for a match - and finds two.

            --
            Old Faithful

            Comment

            Working...