开发者

Why I cant return Boost::Scoped_ptr from function?

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<PublicProducerPro开发者_C百科totype> 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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜