Boost logo

Boost :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2006-12-01 15:16:36

Sohail Somani wrote:
> Thanks for your reply but the domain of COM objects lends itself well to shared_ptr.

As to scoped_ptr. I don't quite understand the "but" in that sentence.

> I went through the archives after the posts and I have realized that
> scoped_ptr is not changing! I will still disagree that the deleter shouldn't
> be a parameterized type.

What's wrong with:

    template<typename T> void free_resource(T* ptr)
    { delete ptr; }
    void free_resource(std::FILE* f)
    { std::fclose(f); }
    void free_resource(IDispatch * o)
    { o->Release(); }

    template<typename T>
    struct scoped_ptr
        // [...]
       ~scoped_ptr() { free_resource(this->p_); }
        // [...]


Adding another dummy parameter will make ADL work (see attached file for
demonstration of the technique).

> The only other option is using a type erasure mechanism like shared_ptr which
> is more trouble than its worth for things like deallocating C resources.

I was referring to the file pointer, which is pretty much the only "C resource" I use.
You're right when it comes to malloc/free, of course - but do we really need it?



#include <iostream>

namespace framework
  enum adl_hook { use_adl };

  template<typename T>
  inline void visit(T const & o, adl_hook)
    std::cout << "default case" << std::endl;

namespace my_prj
  struct my_class { };
  void visit(my_class const & x, framework::adl_hook)

int main()
  int a = 1;
  my_prj::my_class b;

  visit(a, framework::use_adl);
  visit(b, framework::use_adl);

  return 0;

Boost list run by bdawes at, gregod at, cpdaniel at, john at