c++ - Conversion of boost::scoped_ptr to reference fails -
can explain why conversion reference type fails in example? want use boost::scoped_ptr
as workaround missing std::unique_ptr
.
struct a{}; struct b { b(boost::scoped_ptr<a>& a) { m_a.swap(a); } boost::scoped_ptr<a> m_a; }; void f() { b b(boost::scoped_ptr<a>(new a)); }
gcc fail with:
note: no known conversion argument 1 ‘boost::scoped_ptr<a>’ ‘boost::scoped_ptr<a>&’
design rationale (why use scoped_ptr in first place):
- don't want use raw pointers because of exception safety (real-world scenario involves multiple objects passed)
- don't want use auto_ptr because of possible silent deallocation fails
- can't use unique_ptr because still stuck c++03
- use of shared_ptr may valid workaround seems unclean because nothing shared, really
you can't bind non-const lvalue references temporaries, here
b b(boost::scoped_ptr<a>(new a));
you can fix particular problem passing lvalue:
void f() { boost::scoped_ptr<a> a(new a); b b(a); }
Comments
Post a Comment