4

So I try to create some wrapper around boost.extension functions for class creation. So I have 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 calls :

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 last is not so important. What is important - I can not get my function return=(

I try such way :

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

I also have tried :

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

but compiler talls me C2248 it can not call some private member of boost::scoped_ptr<T>

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

James McNellis
  • 348,265
  • 75
  • 913
  • 977
Rella
  • 65,003
  • 109
  • 363
  • 636
  • 3
    My guess, without knowing more about the error, is that the copy constructor is private (i.e. the class is intended to be noncopyable) and the return requires making a copy. You're probably not using the right smart pointer for the job. – Steve Howard Apr 20 '11 at 00:00

2 Answers2

21

boost::scoped_ptr is noncopyable. Since you can't copy a scoped_ptr, you can't return one either (returning an object by value requires that you are able to make a copy of it, at least in C++03). If you need to return an object owned by a smart pointer, you need to pick a different type of smart pointer.

If your compiler supports std::unique_ptr you should use that instead (Since it looks like you're 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 what your exact use case is.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 3
    If you go with `auto_ptr` (which is basically just a broken `unique_ptr`), keep in mind its problems - mainly, that it's not compatible with the STL containers such as `vector` and `map`. Personally, I would recommend `shared_ptr` (which is [part of boost](http://www.boost.org/doc/libs/1_46_1/libs/smart_ptr/smart_ptr.htm)) – BlueRaja - Danny Pflughoeft Apr 20 '11 at 00:07
  • You can either use `boost::shared_ptr` or `std::auto_ptr`. – James McNellis Apr 20 '11 at 00:07
5

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

tony
  • 3,737
  • 1
  • 17
  • 18