Topic: Smart Pointers" Not So Smart?


Author: Branko Cibej <branko.cibej@hermes.si>
Date: 1997/06/11
Raw View
Sowmy Srinivasan wrote:

> How can I embed a fn ptr in auto_ptr and make the indirect fn call
> transparently
> eg.
> auto_ptr<DWORD(*)(int)> pFn = GetFuncPtr();
> DWORD res = pFn(1);
>
> I guess the above statement will not compile unless auto_ptr has
> overloaded operator(). But how can I templatize the
> operator().(Remember
> the number of arguments the func takes is decided by the template
> parameter)


I don't quite understand the problem here. An auto_ptr owns dynamically
allocated objects and automatically deletes them when going out of scope. A
function is not an object, is never created dynamically and can't be
deleted --- s why would you want to store a pointer to a function in an
auto_ptr?

    Regards,
        Brane
--
Branko Cibej   <branko.cibej@hermes.si>
HERMES SoftLab, Litijska 51, 1000 Ljubljana, Slovenia
phone: (++386 61) 186 53 49  fax: (++386 61) 186 52 70
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Paul D. DeRocco" <pderocco@ix.netcom.crud.com>
Date: 1997/06/12
Raw View
Branko Cibej wrote:

> I don't quite understand the problem here. An auto_ptr owns dynamically
> allocated objects and automatically deletes them when going out of scope. A
> function is not an object, is never created dynamically and can't be
> deleted --- s why would you want to store a pointer to a function in an
> auto_ptr?

I think he might be trying to use auto_ptr to refer to a temporarily
created array of bytes that contains a thunk, such as is often used for
callbacks in 16-bit Windows. But that would require that auto_ptr refer
to a struct containing a function pointer (and perhaps forwarding
operator() to the function), instead of a bare function pointer.

--

Ciao,
Paul

(Please remove the extra "crud" from the return address,
which has been altered to foil junk mail senders.)
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Sowmy Srinivasan <KSx_Sowmy@ccm.jf.intel.com>
Date: 1997/06/05
Raw View
How can I embed a fn ptr in auto_ptr and make the indirect fn call
transparently
eg.
auto_ptr<DWORD(*)(int)> pFn = GetFuncPtr();
DWORD res = pFn(1);

I guess the above statement will not compile unless auto_ptr has
overloaded operator(). But how can I templatize the operator().(Remember
the number of arguments the func takes is decided by the template
parameter)
Any kind of help would be greatly appreciated.

Thanks
sowmy
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/06/05
Raw View
Sowmy Srinivasan <KSx_Sowmy@ccm.jf.intel.com> wrote:
: How can I embed a fn ptr in auto_ptr and make the indirect fn call
: transparently
: eg.
: auto_ptr<DWORD(*)(int)> pFn = GetFuncPtr();
: DWORD res = pFn(1);

auto_ptr<> won't work. Not only auto_ptr<> doen't have operator(),
but I doubt you want to delete the pointee :-).
Use pointer_to_unary_function<> (20.3.7) instead.

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Sowmy Srinivasan <KSx_Sowmy@ccm.jf.intel.com>
Date: 1997/06/05
Raw View
Oleg Zabluda wrote:
>
> Sowmy Srinivasan <KSx_Sowmy@ccm.jf.intel.com> wrote:
> : How can I embed a fn ptr in auto_ptr and make the indirect fn call
> : transparently
> : eg.
> : auto_ptr<DWORD(*)(int)> pFn = GetFuncPtr();
> : DWORD res = pFn(1);
>
> auto_ptr<> won't work. Not only auto_ptr<> doen't have operator(),
> but I doubt you want to delete the pointee :-).
> Use pointer_to_unary_function<> (20.3.7) instead.
>
> Oleg.
I am sorry for not making myself clear. Actually I was looking for some
template definition which would be a wrapper to a function pointer (that
takes any type and returns any type) My example is almost correct except
that I used a auto_ptr. Consider a custom pointer-wrapper instead of
auto_ptr. Assume that the custom wrapper does not delete. (See below)

template <class T >
class CApiPtr {
protected:
  T* m_pApi;
public:
  CApiPtr(HMODULE hModule, LPTSTR szApi){
    m_pApi = (T*)GetProcAddress(hModule, szApi);
  }
  operator T*(){ return m_pApi;}
};

Now I can call as below

CApiPtr<DWORD(*)(int)> pFn (hKernel, "GetSystemMetrics");
DWORD res = (*pFn)(1);

But what I was looking for is someway to call as

CApiPtr<DWORD(*)(int)> pFn (hKernel, "GetSystemMetrics");
DWORD res = pFn(1);

THanks
Sowmy
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: "Matt Arnold" <marnold@don't.spam.netcom.com>
Date: 1997/06/08
Raw View
Oleg Zabluda <zabluda@math.psu.edu> wrote in article
<5n7c3p$219e@r02n01.cac.psu.edu>...
> Sowmy Srinivasan <KSx_Sowmy@ccm.jf.intel.com> wrote:
> : How can I embed a fn ptr in auto_ptr and make the indirect fn call
> : transparently
> : eg.
> : auto_ptr<DWORD(*)(int)> pFn = GetFuncPtr();
> : DWORD res = pFn(1);

You can't.  auto_ptr isn't designed for this.  auto_ptr is *not* a functor
(that is, it does not define an operator()).

Also, how is auto_ptr supposed to call delete (as it does in its dtor) on
such a function pointer?


Best regards,
Matt Arnold
Boston, USA :: http://python.ics.uci.edu/~marnold/
C++/MIDI/Win32/95 developer :: e/DM/7R7/RF/C&E/A-Box

Remove "don't.spam." prefix to obtain my valid email address.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]