#include <boost/smart_ptr.hpp>
class Base {
};
class Derived : public Base {
public:
Derived() : Base() {}
};
void func(/*const*/ boost::shared_ptr<Base>& obj) {
}
int main() {
boost::shared_ptr<Base> b;
boost::shared_ptr<Derived> d;
func(b);
func(d);
}
This compiles with the const in func’s signature but not without it. The error appears in the line with the call func(d);
Any hints for me?
When reading the documentation of
boost::shared_ptrwe find the following:This means that
boost::shared_ptr<Derived>is implicitly convertable to an object of typeboost::shared_ptr<Base>.When this conversion takes place upon executing
func (d)a temporary will be created, though non-const references cannot be bound to temporary objects – which is why your compiler issues an error unless you make the argument tofuncaconst&.