SmartPtr type conversion error

Discussion in 'C++' started by tsar.omen@gmail.com, Dec 30, 2005.

  1. Guest

    Hello everyone,
    during my coding practice I have encounted the following problem:

    Vital code pieces:


    ---
    template <class T> class SmartPtr

    {

    private:

    Reference<T>* ref; // object with pointer to T and ref counter

    public:

    SmartPtr() : ref(0) {}

    SmartPtr(T* t) : ref(new Reference<T>(t)) {}

    SmartPtr(SmartPtr& h) : ref(h.acquire()) {}

    SmartPtr& operator = (T* t) {/**/}

    SmartPtr& operator = (SmartPtr& h) {/**/}

    ~SmartPtr() { freeReference(); }

    private:

    void freeReference() {/**/}

    Reference<T>* acquire () {/**/}

    };
    ---


    Now to introduce the problem:

    (1) works:
    SmartPtr<Foo> pFoo(new Foo()); // direct initialization
    (SmartPtr::SmartPtr(T*))

    (2) works:
    SmartPtr<Foo> pFoo1(new Foo());
    SmartPtr<Foo> pFoo2(pFoo1); // direct initialization
    (SmartPtr::SmartPtr(SmartPtr&))

    (3) works:
    SmartPtr<Foo> pFoo; // SmartPtr::SmartPtr()
    pFoo = new Foo(); // operator=(T *)
    pFoo = pFoo7; // operator=(SmartPtr&)

    !(4) does _NOT_ work:

    SmartPtr<Foo> pFoo = new Foo();

    It produces the following error on gcc 3.3.6:

    ---
    main.cc:24: error: no matching function for call to
    `SmartPtr<Point>::SmartPtr( SmartPtr<Point>)'
    smartptr.h:162: error: candidates are:
    SmartPtr<T>::SmartPtr(SmartPtr_ref<T>)
    [with T = Point]
    smartptr.h:73: error:
    SmartPtr<T>::SmartPtr(SmartPtr<T>&) [with T = Point]
    smartptr.h:70: error: SmartPtr<T>::SmartPtr(T*) [with T
    =
    Point]
    main.cc:24: error: initializing temporary from result of `
    SmartPtr<T>::SmartPtr(T*) [with T = Point]'
    ---

    AFAIK in case 'SmartPtr<Foo> pFoo = new Foo();' a copy initialization
    is used. That's firstly applying conversion operator to the right side
    and then calling a copy constructor. Because right side in in fast T* t
    so SmartPtr::SmartPtr(T*) was chosen, resulting in creation of
    temporary object and trying to copy initialize pFoo with tmp object
    which fails [SmartPtr<Point>::SmartPtr(SmartPtr<Point>) as reported by
    g++].

    My question is if it's possible to somehow fix it to allow construction
    like 'SmartPtr<Foo> pFoo = new Foo();' legal.

    Many thanks in advance.

    --
    zakkath
     
    , Dec 30, 2005
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Hello everyone,
    > during my coding practice I have encounted the following problem:
    >
    > Vital code pieces:
    >
    >
    > ---
    > template <class T> class SmartPtr
    >
    > {
    >
    > private:
    >
    > Reference<T>* ref; // object with pointer to T and ref counter
    >
    > public:
    >
    > SmartPtr() : ref(0) {}
    >
    > SmartPtr(T* t) : ref(new Reference<T>(t)) {}
    >
    > SmartPtr(SmartPtr& h) : ref(h.acquire()) {}
    >
    > SmartPtr& operator = (T* t) {/**/}
    >
    > SmartPtr& operator = (SmartPtr& h) {/**/}
    >
    > ~SmartPtr() { freeReference(); }
    >
    > private:
    >
    > void freeReference() {/**/}
    >
    > Reference<T>* acquire () {/**/}
    >
    > };
    > ---
    >
    >
    > Now to introduce the problem:
    >
    > (1) works:
    > SmartPtr<Foo> pFoo(new Foo()); // direct initialization
    > (SmartPtr::SmartPtr(T*))
    >
    > (2) works:
    > SmartPtr<Foo> pFoo1(new Foo());
    > SmartPtr<Foo> pFoo2(pFoo1); // direct initialization
    > (SmartPtr::SmartPtr(SmartPtr&))
    >
    > (3) works:
    > SmartPtr<Foo> pFoo; // SmartPtr::SmartPtr()
    > pFoo = new Foo(); // operator=(T *)
    > pFoo = pFoo7; // operator=(SmartPtr&)
    >
    > !(4) does _NOT_ work:
    >
    > SmartPtr<Foo> pFoo = new Foo();
    >
    > It produces the following error on gcc 3.3.6:
    >
    > ---
    > main.cc:24: error: no matching function for call to
    > `SmartPtr<Point>::SmartPtr( SmartPtr<Point>)'
    > smartptr.h:162: error: candidates are:
    > SmartPtr<T>::SmartPtr(SmartPtr_ref<T>)
    > [with T = Point]
    > smartptr.h:73: error:
    > SmartPtr<T>::SmartPtr(SmartPtr<T>&) [with T = Point]
    > smartptr.h:70: error: SmartPtr<T>::SmartPtr(T*) [with T
    > =
    > Point]
    > main.cc:24: error: initializing temporary from result of `
    > SmartPtr<T>::SmartPtr(T*) [with T = Point]'
    > ---
    >
    > AFAIK in case 'SmartPtr<Foo> pFoo = new Foo();' a copy initialization
    > is used. That's firstly applying conversion operator to the right side
    > and then calling a copy constructor. Because right side in in fast T* t
    > so SmartPtr::SmartPtr(T*) was chosen, resulting in creation of
    > temporary object and trying to copy initialize pFoo with tmp object
    > which fails [SmartPtr<Point>::SmartPtr(SmartPtr<Point>) as reported by
    > g++].
    >
    > My question is if it's possible to somehow fix it to allow construction
    > like 'SmartPtr<Foo> pFoo = new Foo();' legal.
    >
    > Many thanks in advance.
    >
    > --
    > zakkath


    This is an issue of constness and an optimization. Specifically, some
    initialization optimization is taking place in your fourth example but
    because your parameter to the copy constructor is not const, you cannot
    bind an rvalue to it. In short, you need to add const as such:

    SmartPtr( const SmartPtr& h) : ref(h.acquire()) {}

    Reference<T>* acquire() const {/**/}

    Cheers! --M
     
    mlimber, Dec 30, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. luna
    Replies:
    1
    Views:
    6,838
  2. ibiza
    Replies:
    2
    Views:
    5,363
    ibiza
    Jan 27, 2006
  3. mosfet

    SmartPtr Issues

    mosfet, Aug 12, 2008, in forum: C++
    Replies:
    1
    Views:
    411
    Michael DOUBEZ
    Aug 12, 2008
  4. Mosfet

    Loki::SmartPtr syntax

    Mosfet, Apr 10, 2009, in forum: C++
    Replies:
    1
    Views:
    853
    Alf P. Steinbach
    Apr 10, 2009
  5. dbtouch

    Threaded SmartPtr questions

    dbtouch, May 6, 2009, in forum: C++
    Replies:
    1
    Views:
    275
    James Kanze
    May 7, 2009
Loading...

Share This Page