Can't make std::pair with std::auto_ptr as a member type with gcc

closed account (1yR4jE8b)
Here is a snippet representing my problem:

1
2
3
4
5
int main(void)
{
  std::pair<std::auto_ptr<int>, int> x (std::auto_ptr<int> j(new int(0)), 2);
  return 0;
};


And here is the error that gcc produces:


In file included from c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_algobase.h:66:0,
                 from c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/char_traits.h:41,
                 from c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/ios:41,
                 from c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/ostream:40,
                 from c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/iostream:40,
                 from main.cpp:1:
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h: In constructor 'std::pair<_T1, _T2>::pair(const _T1&, const _T2&) [with _T1 = std::auto_ptr<tjc::particle>, _T2 = std::list<tjc::particle*>]':
particle_system.hpp:62:101:   instantiated from 'void tjc::particle_system<T>::add(const tjc::particle&) [with particle_type = tjc::particle2d]'
main.cpp:19:40:   instantiated from here
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h:88:31: error: no matching function for call to 'std::auto_ptr<tjc::particle>::auto_ptr(const std::auto_ptr<tjc::particle>&)'
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/backward/auto_ptr.h:258:7: note: candidates are: std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr_ref<_Tp>) [with _Tp = tjc::particle]
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/backward/auto_ptr.h:110:7: note:                 std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&) [with _Tp = tjc::particle, std::auto_ptr<_Tp> = std::auto_ptr<tjc::particle>]
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/backward/auto_ptr.h:101:7: note:                 std::auto_ptr<_Tp>::auto_ptr(element_type*) [with _Tp = tjc::particle, element_type = tjc::particle]
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h: In constructor 'std::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = std::auto_ptr<tjc::particle>, _U2 = std::list<tjc::particle*>, _T1 = std::auto_ptr<tjc::particle>, _T2 = std::list<tjc::particle*>]':
particle_system.hpp:62:101:   instantiated from 'void tjc::particle_system<T>::add(const tjc::particle&) [with particle_type = tjc::particle2d]'
main.cpp:19:40:   instantiated from here
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_pair.h:116:21: error: no matching function for call to 'std::auto_ptr<tjc::particle>::auto_ptr(const std::auto_ptr<tjc::particle>&)'
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/backward/auto_ptr.h:258:7: note: candidates are: std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr_ref<_Tp>) [with _Tp = tjc::particle]
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/backward/auto_ptr.h:110:7: note:                 std::auto_ptr<_Tp>::auto_ptr(std::auto_ptr<_Tp>&) [with _Tp = tjc::particle, std::auto_ptr<_Tp> = std::auto_ptr<tjc::particle>]
c:\apps\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/backward/auto_ptr.h:101:7: note:                 std::auto_ptr<_Tp>::auto_ptr(element_type*) [with _Tp = tjc::particle, element_type = tjc::particle]


This snippet compiles fine with Visual Studio 2010.

The problem occurs in an STL header. In the gnu implementation, the constructor of std::pair passes the objects by const reference, an error occurs when trying to copy a const auto_ptr to the non-const auto_ptr in the pair.
Last edited on
Yes, the problem is that the auto_ptr copy constructor takes a non
const reference argument, because it transfers memory ownership.

EDIT: boost::shared_ptr works fine.
Last edited on
Topic archived. No new replies allowed.