Hello,
I'm upgrading from boost 1.33.1 to 1.34.1 and upgrade does not let me
compile the following code:
// CRefCounted provides functionality to CRefPtr to have
// referenced pointers
struct CNode: public CRefCounted
{
void GetName(){}
};
int main(int argc, char **argv){
typedef CRefPtr<CNode> CPNode;
typedef std::multimap TPNodes;
TPNodes Children;
std::for_each(
Children.begin(),
Children.end(),
boost::bind(
&CNode::GetName,
// problem in the next line
boost::bind(&TPNodes::value_type::second,_1)));
return 0;
}
I get the following messages from MSVC8.0:
error C2784: 'T *boost::get_pointer(const boost::shared_ptr<T> &)' :
could not deduce template argument for 'const boost::shared_ptr<T> &'
from 'const CPNode'
error C2784: 'T *boost::get_pointer(const std::auto_ptr<_Ty> &)' : could
not deduce template argument for 'const std::auto_ptr<_Ty> &' from
'const CPNode'
error C2784: 'T *boost::get_pointer(T *)' : could not deduce template
argument for 'T *' from 'const CPNode'
If I replace CRefPtr with boost::shared_ptr the code compiles. Hence I
assume the problem is with the way I implemented CRefPtr class. Here it is:
template<typename T> class CRefPtr {
private:
T* m_ptr;
public:
CRefPtr(T* ptr=NULL)
:m_ptr(NULL){
*this=ptr;
}
CRefPtr(const CRefPtr<T>& ptr)
:m_ptr(NULL){
*this=ptr;
}
~CRefPtr() {
if (m_ptr) {
m_ptr->DecrementReference();
m_ptr=NULL;
}
}
CRefPtr<T>& operator=(T* ptr) {
if(ptr!=m_ptr) {
if(m_ptr) {
m_ptr->DecrementReference();
}
m_ptr=ptr;
if(m_ptr) {
m_ptr->IncrementReference();
}
}
return *this;
}
CRefPtr<T>& operator=(const CRefPtr<T>& ptr){*this=ptr.m_ptr;return *this;}
T* operator->() const{return m_ptr;}
T& operator*() const{return *m_ptr;}
bool operator<(const CRefPtr<T>&
obj)const{if(obj.m_ptr<this->m_ptr)return true;else return false;}
T* get() const{return m_ptr;}
operator T*()const{return m_ptr;}
};
What am I missing?
Some explanation on why I went this route. I am using CRefPtr instead of
shared_ptr because full CNode class has a member CNode* m_Parent. I
could not figure out how to use weak pointers, but if I use CRefPtr I
can assign a simple pointer returned by GetParent() to a CPNode and it
would still work, while I could not achieve this result with shared_ptr.
Maybe someone can direct me to a place which explains how to handle such
situations properly with shared_ptr.
Thanks.
--
Regards, Alexander. http://sjcomp.com