Why can't I return Boost :: Scoped_ptr from a function?

So I'm trying to create some wrapper around boost.extension functions to create a class. So I created a function:

template <class BaseClass, class ConstructorType> boost::scoped_ptr<BaseClass> get_class (shared_library & lib, std::string class_name, ConstructorType value ) { map<string, factory<BaseClass, ConstructorType> > lib_factories = get_factories<BaseClass, ConstructorType>(lib); return boost::scoped_ptr<BaseClass> lib_class(lib_factories[class_name].create(value)); } 

which causes:

 template <class BaseClass, class ConstructorType> map<string, factory<BaseClass, ConstructorType> > get_factories (shared_library & lib) { type_map lib_types; if (!lib.call(lib_types)) { cerr << "Types map not found!" << endl; cin.get(); } map<string, factory<BaseClass, ConstructorType> > lib_factories(lib_types.get()); if (lib_factories.empty()) { cerr << "Producers not found!" << endl; cin.get(); } return lib_factories; } 

but the latter is not so important. What is important - I can not get my function return = (

I try like this:

 boost::scoped_ptr<PublicProducerPrototype> producer = get_class<PublicProducerPrototype, int>(simple_producer, "simpleProducer", 1); 

I also tried:

 boost::scoped_ptr<PublicProducerPrototype> producer ( get_class<PublicProducerPrototype, int>(simple_producer, "simpleProducer", 1)); 

but the compiler changes t C2248 , it cannot call some private member boost::scoped_ptr<T>

So how to make my return ... returnable // how to get it?

+4
source share
2 answers

boost::scoped_ptr is not boost::scoped_ptr . Since you cannot copy scoped_ptr , you cannot return it (returning an object by value requires that you can make a copy of it, at least in C ++ 03). If you need to return an object that belongs to a smart pointer, you need to select a different type of smart pointer.

If your compiler supports std::unique_ptr , you should use it instead (since it looks like you are using Visual C ++, Visual C ++ 2010 supports std::unique_ptr ); otherwise, consider using std::auto_ptr or {std,std::tr1,boost}::shared_ptr , depending on your specific use case.

+20
source

You can also try boost :: interprocess :: unique_ptr.

+5
source

All Articles