Thread: Problem with template

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    399

    Problem with template

    I want to write an auto_ptr implementation for Core Foundation objects, but I'm having a little problem with making a converting constructor.

    Code:
    template <typename T>
    class CF_auto_ptr
    {
    	public:
    		CF_auto_ptr(T p = 0);
    		~CF_auto_ptr();
    		CF_auto_ptr<T>(CF_auto_ptr<T>& rhs);
    		CF_auto_ptr<T>& operator=(CF_auto_ptr<T> rhs);
    		
    		T get() const;
    		void release();
    		void reset(T p);
    	private:
    		T p;
    };
    
    template <typename T>
    CF_auto_ptr<T>::CF_auto_ptr(T p)
    	: p(p)
    {}
    
    template <typename T>
    CF_auto_ptr<T>::~CF_auto_ptr()
    {
    	if (p)
    	{
    		CFRelease(p);
    	}
    }
    
    template <typename T>
    CF_auto_ptr<T>::CF_auto_ptr(CF_auto_ptr<T>& rhs)
    {
    	p = rhs.get();
    	rhs.release();
    }
    
    template <typename T>
    CF_auto_ptr<T>& CF_auto_ptr<T>::operator=(CF_auto_ptr<T> rhs)
    {
    	p = rhs.get();
    	rhs.release();
    	return *this;
    }
    I thought this would do it, but I get errors when I use it like this:
    Code:
    CF_auto_ptr<CFBundleRef> main_bundle = CFBundleGetMainBundle();
    Code:
    error: no matching function for call to 'CF_auto_ptr<__CFBundle*>::CF_auto_ptr(CF_auto_ptr<__CFBundle*>)'
    Code:
    candidates are: CF_auto_ptr<T>::CF_auto_ptr(CF_auto_ptr<T>&) [with T = __CFBundle*]

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The problem is that if you accept an argument by non-const reference, you can't pass a temporary to it.

    I guess, here you could call your other constructor:

    Code:
    CF_auto_ptr<CFBundleRef> main_bundle(CFBundleGetMainBundle());
    In any case, mimicking the semantics of std::auto_ptr (ownership transfer) might not be worth it (it is practically a deprecated class).

    Instead, if you want, you should be easily able to share resources with boost::shared_ptr (or std::tr1::shared_ptr), which allow to use a custom deleter. Or you might just make you auto_ptr non-copyable (like std::tr1::unique_ptr or boost::scoped_ptr).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Template object creation problem
    By babu198649 in forum C++ Programming
    Replies: 7
    Last Post: 09-16-2008, 04:02 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM
  5. Problem with template usage
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2002, 06:30 PM