0

如何map<string, factory<BaseClass, ConstructorType> >从这样的功能中接收?

所以我有

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;

}

我尝试通过以下方式获得它的价值:

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

我尝试为自己概括/简化一些 boost.extension 方法。

那么如何map<A, B>&正确接收呢?

如何正确初始化链接或如何返回不是链接而是真实对象?(对不起 C++ nube)

4

1 回答 1

1

如果你需要一个地图的引用,你应该将函数声明为返回一个引用,而不是一个值:

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

当然,这假设返回的引用lib_types.get()可以安全地作为引用传递出去。

于 2011-04-19T23:11:53.670 回答