Problem with template

This is a discussion on Problem with template within the C++ Programming forums, part of the General Programming Boards category; I want to write an auto_ptr implementation for Core Foundation objects, but I'm having a little problem with making a ...

  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, 05:02 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 02:03 AM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  4. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM
  5. Problem with template usage
    By rmullen3 in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2002, 06:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21