c++functionboostdictionaryboost-extension

how to receive map<A, B>& from function/class?


How to receive map<string, factory<BaseClass, ConstructorType> > from such function?

So I have

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;

    }

    map<string, factory<BaseClass, ConstructorType> >& lib_factories(lib_types.get());
    if (lib_factories.empty()) {
        cerr << "Producers not found!" << endl;

    }
    return lib_factories;

}

and I try to get its value with something like:

map<string, factory<PublicProducerPrototype, int> > producer_factories();
producer_factories = get_factories<PublicProducerPrototype, int>(simple_producer);

I try to generalize/simplify some of boost.extension methods for myself.

So how to receive map<A, B>& correctly?

How to initialize link correctly or how to return not link but real object? (sorry C++ nube)


Solution

  • If you need a reference to a map, you should declare the function as returning a reference, not a value:

    template <class BaseClass, class ConstructorType>
    map<string, factory<BaseClass, ConstructorType> >& get_factories (...
    

    Of course, this assumes that the reference returned by lib_types.get() is safe to pass out as a reference.