gcc解决方法,同时使用shared_ptr在std :: set中插入 [英] gcc workaround while using shared_ptr to insert in std::set

查看:148
本文介绍了gcc解决方法,同时使用shared_ptr在std :: set中插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码无法使用g ++ 4.2.1编译,但在vc ++ v8下工作正常。

  #include< set& 
typedef std :: set< int *> IntPtrSet;
IntPtrSet iptrSet;

typedef std :: set< shared_ptr< int>> IntPtrSet2;
IntPtrSet2 iptrSet2;

void AddIntegers(int& x)
{
iptrSet.insert(& x);
iptrSet2.insert(& x);
}

shared_ptr类似于boost :: shared_ptr或tr1 :: shared_ptr。 p>

它会产生以下错误:

 没有匹配的函数调用std :: allocator< shared_ptr< int>> :: construct(int **,const shared_ptr< int>&)
没有匹配的函数调用std :: allocator< shared_ptr< int& > :: destroy(int **)



以下是完整的错误讯息:

  /Developer/SDKs/MacOSX10.6.sdk/usr/include/c ++ / 4.2.1 / bits / stl_tree.h:402:错误:没有匹配的函数调用'std :: allocator< shared_ptr< int> ; > :: destroy(int **)'

/Users/mark/Templates/Function/main.cpp:188:从这里实例化
/Developer/SDKs/MacOSX10.6。 sdk / usr / include / c ++ / 4.2.1 / bits / stl_tree.h:380:error:没有匹配的函数调用'std :: allocator< shared_ptr< int& > :: construct(int **,const shared_ptr< int>&)'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h: 106:note:candidates are:void __gnu_cxx :: new_allocator< _Tp> :: construct(_Tp *,const _Tp&)[with _Tp = shared_ptr< int>]

完成模板实例化堆栈:

  / Developer / usr / gcc-4.2 -x c ++ -arch x86_64 -fmessage-length = 0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot 
/ Developer / SDKs / MacOSX10.6.sdk -mfix-and-continue -fvisibility-inlines-hidden -mmacosx-version-min = 10.6 -gdwarf-2 -iquote /Users/mark/Templates/Function/build/Function.build/Debug/Function。 build / Function-generated-files.hmap - I / Users / mark / Templates / Function / build / Function.build / Debug / Function.build / Function-own-target-headers.hmap - I / Users / mark / Function / build / Function.build / Debug / Function.build / Function-all-target-headers.hmap -iquote /Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-project-头文件/目录/用户/标记/模板/函数/构建/调试 - I /用户/标记/模板/函数/ Debug / Function.build / DerivedSources / x86 _64 - I / Users / mark / Templates / Function / build / Function.build / Debug / Function.build / DerivedSources -c /Users/mark/Templates/Function/main.cpp -o /Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Objects- normal / x86_64 / main.o

/Developer/SDKs/MacOSX10.6.sdk/usr /include/c++/4.2.1/bits/stl_tree.h:在成员函数'无效的std :: _ Rb_tree< _key,_Val,_KeyOfValue,_Compare,_Alloc> :: _ M_destroy_node(的std :: _ Rb_tree_node<&_Val GT; *)与_Key = shared_ptr< int>,_Val = shared_ptr< int>,_KeyOfValue = std :: _ Identity< shared_ptr< int> >,_Compare = std :: less< shared_ptr< int> >,_Alloc = std :: allocator< shared_ptr< int> >]':

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:1327:从'void std :: _ Rb_tree< ; _key,_Val,_KeyOfValue,_Compare,_Alloc> :: _ M_erase(STD :: _ Rb_tree_node< _Val> *)[与_key = shared_ptr的< INT>中_Val = shared_ptr的< INT>中_KeyOfValue =的std :: _身份< shared_ptr的< INT> ; >,_Compare = std :: less< shared_ptr< int> >,_Alloc = std :: allocator< shared_ptr< int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:594:从'std :: _ Rb_tree< _Key实例化,_Val,_KeyOfValue,_Compare,_Alloc> ::〜_Rb_tree()[与_key = shared_ptr的< INT>中_Val = shared_ptr的< INT>中_KeyOfValue =的std :: _身份< shared_ptr的< INT> >,_Compare = std :: less< shared_ptr< int> >,_Alloc = std :: allocator< shared_ptr< int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_set.h:141:从'std :: set< _Key实例化,_Compare,_Alloc> :: set()[with _Key = shared_ptr< int>,_Compare = std :: less< shared_ptr< int> >,_Alloc = std :: allocator< shared_ptr< int> >]'

/Users/mark/Templates/Function/main.cpp:181:这里

  /Developer/SDKs/MacOSX10.6.sdk/usr/include/c ++ / 4.2.1 / bits / stl_tree。 h:402:error:没有匹配的函数调用'std :: allocator< shared_ptr< int> > :: destroy(int **)'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:110:note:候选人是:无效__gnu_cxx :: new_allocator<&_TP GT; ::破坏(_TP *)[与_TP = shared_ptr的< INT]的计算值

/Developer/SDKs/MacOSX10.6.sdk/usr/include/ C ++ / 4.2.1 /位/ stl_tree.h:在成员函数'的std :: _ Rb_tree_node< _Val> *的std :: _ Rb_tree< _key,_Val,_KeyOfValue,_Compare,_Alloc> :: _ M_create_node(常量_Val&安培;)与_key = shared_ptr< int>,_Val = shared_ptr< int>,_KeyOfValue = std :: _ Identity< shared_ptr< int> >,_Compare = std :: less< shared_ptr< int> >,_Alloc = std :: allocator< shared_ptr< int> >]':

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:840:从'typename std :: _ Rb_tree< ; _key,_Val,_KeyOfValue,_Compare,_Alloc> ::迭代器的std :: _ Rb_tree< _key,_Val,_KeyOfValue,_Compare,_Alloc> :: _ M_insert(STD :: _ Rb_tree_node_base *,性病:: _ Rb_tree_node_base *,常量_Val和放大器;)与_Key = shared_ptr< int>,_Val = shared_ptr< int>,_KeyOfValue = std :: _ Identity< shared_ptr< int> >,_Compare = std :: less< shared_ptr< int> >,_Alloc = std :: allocator< shared_ptr< int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:988:从'std :: pair< typename std :: _ Rb_tree< _Key,_Val,_KeyOfValue,_Compare,_Alloc> :: iterator,bool>的std :: _ Rb_tree< _key,_Val,_KeyOfValue,_Compare,_Alloc> :: _ M_insert_unique(常量_Val&安培;)与_key = shared_ptr的< INT>中_Val = shared_ptr的< INT>中_KeyOfValue =的std :: _身份< shared_ptr的< INT> >,_Compare = std :: less< shared_ptr< int> >,_Alloc = std :: allocator< shared_ptr< int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_set.h:307:从'std :: pair< typename std :: _ Rb_tree< _Key,_Key,std :: _ Identity< _Key> ;, _Compare,typename _Alloc :: rebind< _Key> :: other> :: const_iterator,bool& std :: set< _Key,_Compare,_Alloc> :: insert(const _Key&)[with _Key = shared_ptr< int> ;, _Compare = std :: less< shared_ptr< int& >,_Alloc = std :: allocator< shared_ptr< int> >]'

/Users/mark/Templates/Function/main.cpp:188:从这里实例化

/Developer/SDKs/MacOSX10.6.sdk/usr /include/c++/4.2.1/bits/stl_tree.h:380:error:没有匹配的函数调用'std :: allocator< shared_ptr< int> > :: construct(int **,const shared_ptr< int>&)'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/ new_allocator.h:106:注意:考生:无效__gnu_cxx :: new_allocator< _TP> ::建设(_TP *,常量_TP和放大器;)与_TP = shared_ptr的< INT]的计算值

以下是完整的代码:

  #include < iostream> 
template< class T>
class shared_ptr
{
private:
T * m_p;
public:
shared_ptr()throw():m_p(NULL){}

shared_ptr(const shared_ptr< T&
m_p = p;
}

shared_ptr(T * p)throw()
{
m_p = p;
}

〜shared_ptr()throw()
{
m_p = NULL;
}

T * operator =(const shared_ptr< T& p)throw()
{
if(m_p!= p.m_p)
{
m_p = p;
}
return m_p;
}

T * operator =(T * p)throw()
{
if(m_p!= p)
{
m_p = p;
}
return m_p;
}

运算符T *()const throw()
{
return m_p;
}

T& operator *()const throw()
{
return * m_p;
}

T **运算符&()throw()
{
return& m_p;
}

bool operator!()const throw()
{
return(m_p == NULL);
}

bool operator<(T * p)const throw()
{
return m_p< p;
}

bool operator!=(int nNull)const throw()
{
return!operator ==(nNull);
}

bool操作符==(int nNull)const throw()
{
return m_p == NULL;
}

bool operator!=(T * p)const throw()
{
return!operator ==(p);
}

bool operator ==(T * p)const throw()
{
return m_p == p;
}

void CopyTo(T ** pp)const throw()
{
* pp = m_p;
}

void Release()throw()
{
T * p = m_p;
if(p)
{
m_p = NULL;
}
}

void Attach(T * p)throw()
{
m_p = p;
}

T * Detach()throw()
{
T * p = m_p;
m_p = NULL;
return p;
}

};

  #include< set> 
typedef std :: set< int *> IntPtrSet;
IntPtrSet iptrSet;

typedef std :: set< shared_ptr< int> > IntPtrSet2;
IntPtrSet2 iptrSet2;

void AddIntegers(int& x)
{
iptrSet.insert(& x);

shared_ptr< int> intPtr(new int(3));

iptrSet2.insert(intPtr);
}


int main(int argc,char * const argv []){


char c;
std :: cin>> C;
return 0;
}

同样在Dinkumware网站上代码编译成功。它无法使用g ++编译如前所述.FYI,我已经粘贴了Dinkumware网站(http://www.dinkumware.com/exam/default.aspx)的结果。



您的代码已使用Microsoft Visual Studio 2005 C ++编译器编译,使用
从Dinkum完全库中为VC ++包Dinkum C ++库。



这是编译器输出使用上面的代码在一个文件名为
sourceFile.cpp:






sourceFile。 cpp






size sourceFile.exe:
6144t + 3584 .rdata + 512d = 10240(2800) p>

代码编译成功!
生成的可执行文件为11 KB。



非常感谢。



$

解决方案

shared_ptr

与boost / tr1相比,T **和T *有额外的转换运算符。隐式转换为底层指针类型(T *)只会混淆编译器。



在我注释掉这些方法后,我不得不修复拷贝构造函数复制成员方式而不是滥用转换运算符。它仍然不能作为共享指针,因为没有引用计数。



然后我不得不修复 operator< const shared_ptr< T> p ,因为隐式转换导致这个编译,但稍后混淆编译器在 std :: set 。它感到困惑,因为它试图构造一个 shared_ptr 但是运算符& (我认为)导致它降级到 T ** 所以类型与容器内的类型不匹配。



以便使用g ++ 4.2成功编译。



编辑:
我可以编写一个自定义分配器,但它似乎不太可怕干净,甚至可能在所有情况下都无法工作。

 模板< class T& 
class sh_ptr_alloc:public std :: allocator< T>
{
public:
typedef size_t size_type;

sh_ptr_alloc()throw(){}

template< typename Tp1>
sh_ptr_alloc(const sh_ptr_alloc< Tp1&)throw(){}

template< typename Tp1>
struct rebind
{typedef sh_ptr_alloc< Tp1>其他; };

template< class Tp1>
void
deallocate(Tp1 * p,size_type)
{:: operator delete(p); }

template< class Tp1>
void
construct(Tp1 ** p,const shared_ptr< Tp1& val)
{:: new(p)shared_ptr< int> }

template< class Tp1>
void
destroy(Tp1 ** p){}
};

然后:



code> typedef std :: set< shared_ptr< int>,std :: less< shared_ptr< int> >,sh_ptr_alloc< shared_ptr< int> > > IntPtrSet2;


This code fails to compile using g++ 4.2.1 but works fine under vc++ v8.

#include <set>
typedef std::set<int *> IntPtrSet;
IntPtrSet iptrSet;

typedef std::set<shared_ptr<int>> IntPtrSet2;
IntPtrSet2 iptrSet2;

void AddIntegers(int& x)
{
    iptrSet.insert(&x);
    iptrSet2.insert(&x);
}

shared_ptr is similar to boost::shared_ptr or tr1::shared_ptr.

It emits the following errors,

No matching function for call to std::allocator<shared_ptr<int>>::construct(int**, const shared_ptr<int>&)
No matching function for call to std::allocator<shared_ptr<int> >::destroy(int **)

Did anyone encounter such error before? If yes, what is the workaround.

Here is the complete error message:

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:402: error: no matching function for call to 'std::allocator<shared_ptr<int> >::destroy(int**)'

/Users/mark/Templates/Function/main.cpp:188:   instantiated from here
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:380: error: no matching function for call to 'std::allocator<shared_ptr<int> >::construct(int**, const shared_ptr<int>&)'
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:106: note: candidates are: void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = shared_ptr<int>]

Complete Template Instantiation stack:

/Developer/usr/bin/gcc-4.2 -x c++ -arch x86_64 -fmessage-length=0 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -O0 -Wreturn-type -Wunused-variable -isysroot 
/Developer/SDKs/MacOSX10.6.sdk -mfix-and-continue -fvisibility-inlines-hidden -mmacosx-version-min=10.6 -gdwarf-2 -iquote   /Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-generated-files.hmap - I/Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-own-target-headers.hmap - I/Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-all- target-headers.hmap -iquote  /Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Function-project- headers.hmap -F/Users/mark/Templates/Function/build/Debug - I/Users/mark/Templates/Function/build/Debug/include - I/Users/mark/Templates/Function/build/Function.build/Debug/Function.build/DerivedSources/x86 _64 - I/Users/mark/Templates/Function/build/Function.build/Debug/Function.build/DerivedSources -c  /Users/mark/Templates/Function/main.cpp -o  /Users/mark/Templates/Function/build/Function.build/Debug/Function.build/Objects- normal/x86_64/main.o

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h: In member function 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_destroy_node(std::_Rb_tree_node<_Val>*) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]':

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:1327:   instantiated from 'void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_erase(std::_Rb_tree_node<_Val>*) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:594:   instantiated from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::~_Rb_tree() [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_set.h:141:   instantiated from 'std::set<_Key, _Compare, _Alloc>::set() [with _Key = shared_ptr<int>, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Users/mark/Templates/Function/main.cpp:181: instantiated from here

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:402: error: no matching function for call to 'std::allocator<shared_ptr<int> >::destroy(int**)'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:110: note: candidates are: void __gnu_cxx::new_allocator<_Tp>::destroy(_Tp*) [with _Tp = shared_ptr<int>]

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h: In member function 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(const _Val&) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]':

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:840:   instantiated from 'typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert(std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, const _Val&) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:988:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator, bool> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_insert_unique(const _Val&) [with _Key = shared_ptr<int>, _Val = shared_ptr<int>, _KeyOfValue = std::_Identity<shared_ptr<int> >, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_set.h:307:   instantiated from 'std::pair<typename std::_Rb_tree<_Key, _Key, std::_Identity<_Key>, _Compare, typename _Alloc::rebind<_Key>::other>::const_iterator, bool> std::set<_Key, _Compare, _Alloc>::insert(const _Key&) [with _Key = shared_ptr<int>, _Compare = std::less<shared_ptr<int> >, _Alloc = std::allocator<shared_ptr<int> >]'

/Users/mark/Templates/Function/main.cpp:188:   instantiated from here

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/bits/stl_tree.h:380: error: no matching function for call to 'std::allocator<shared_ptr<int> >::construct(int**, const shared_ptr<int>&)'

/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/ext/new_allocator.h:106: note: candidates are: void __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, const _Tp&) [with _Tp = shared_ptr<int>]

Here is the complete code:

#include <iostream>
template <class T>
class shared_ptr
{
    private:
    T* m_p;
public:
shared_ptr() throw() : m_p(NULL){}

shared_ptr( const shared_ptr<T>& p) throw()
{
      m_p = p;
}

shared_ptr( T* p) throw()
{
    m_p = p;
}

~shared_ptr() throw()
{
    m_p = NULL;
}

T* operator=(const shared_ptr<T>& p) throw()
{
    if (m_p != p.m_p)
    {
        m_p = p;
    }
    return m_p;
}

T* operator=(T* p) throw()
{
    if (m_p != p)
    {
        m_p = p;
    }
    return m_p;
}

operator T*() const throw()
{
    return m_p;
}

T& operator*() const throw()
{
    return *m_p;
}

T** operator&() throw()
{
    return &m_p;
}

bool operator!() const throw()
{
    return (m_p == NULL);
}

bool operator<(T* p) const throw()
{
    return m_p < p;
}

bool operator!=(int nNull) const throw()
{
    return !operator==(nNull);
}

bool operator==( int nNull) const throw()
{
    return m_p == NULL;
}

bool operator!=( T* p) const throw()
{
    return !operator==(p);
}

bool operator==( T* p) const throw()
{
    return m_p == p;
}

void CopyTo( T** pp) const throw()
{
    *pp = m_p;
}

void Release() throw()
{
    T* p = m_p;
    if (p)
    {
        m_p = NULL;
    }
}

void Attach( T* p) throw()
{
    m_p = p;
}

T* Detach() throw()
{
    T* p = m_p;
    m_p = NULL;
    return p;
}

};

#include <set>
typedef std::set<int *> IntPtrSet;
IntPtrSet iptrSet;

typedef std::set<shared_ptr<int> > IntPtrSet2;
IntPtrSet2 iptrSet2;

void AddIntegers(int& x)
{
iptrSet.insert(&x);

shared_ptr<int> intPtr(new int(3));

iptrSet2.insert(intPtr);
}


int main (int argc, char * const argv[]) {


char c;
std::cin >> c;
    return 0;
}

Also on the Dinkumware site the code compiled successfully. It fails to compile using g++ as mentioned earlier.FYI, I have pasted the result from the Dinkumware website (http://www.dinkumware.com/exam/default.aspx) below.

Your code has been compiled with the Microsoft Visual Studio 2005 C++ compiler using the Dinkum C++ library from the Dinkum Compleat Libraries for VC++ package.

This is the compiler output using the code above in a file named sourceFile.cpp:


sourceFile.cpp


size sourceFile.exe : 6144t + 3584 .rdata + 512d = 10240 (2800)

Code compiled successfully! The executable generated was 11 KB.

Thanks a lot

Regards, Mark

解决方案

Your shared_ptr implementation seems very unusual.

Compared to boost/tr1, you have extra converting operators for T** and T*. Implicitly converting to the underlying pointer type (T*) is only going to confuse the compiler.

After I commented out those methods, I had to fix the copy constructor to actually copy member-wise rather than abusing the converting operator. It still won't work as a shared pointer because there's no reference counting.

Then I had to fix operator< to take const shared_ptr<T>& p again because the implicit conversions were causing this to compile, but confusing the compiler on the std::set later on. It got confused because it was trying to construct a shared_ptr but the operator& (I think) caused it to degrade to T** so the type didn't match the type inside the container.

With those changes I was able to get it to successfully compile with g++ 4.2.

EDIT: Well, I was able to write a custom allocator that compiles but it doesn't seem terribly clean and may not even work in all cases.

template <class T>
class sh_ptr_alloc : public std::allocator<T>
{
public:
        typedef size_t     size_type;

      sh_ptr_alloc() throw() { }

      template<typename Tp1>
        sh_ptr_alloc(const sh_ptr_alloc<Tp1>&) throw() { }

      template<typename Tp1>
        struct rebind
        { typedef sh_ptr_alloc<Tp1> other; };

template <class Tp1>
      void
      deallocate(Tp1* p, size_type)
      { ::operator delete(p); }

template <class Tp1>
      void 
      construct(Tp1** p, const shared_ptr<Tp1>& val) 
      { ::new(p) shared_ptr<int>(val); }

template <class Tp1>
      void 
      destroy(Tp1** p) { }
};

The set is then:

typedef std::set<shared_ptr<int>, std::less<shared_ptr<int> >, sh_ptr_alloc<shared_ptr<int> > > IntPtrSet2;

这篇关于gcc解决方法,同时使用shared_ptr在std :: set中插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆