c++ - 如何从函数/类接收 map<A, B>&?

标签 c++ function boost dictionary boost-extension

如何接收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;

}

我尝试通过类似的方式获取它的值(value):

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

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

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

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

最佳答案

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

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

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

关于c++ - 如何从函数/类接收 map<A, B>&?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5723739/

相关文章:

c++ - 在 C 或 C++ 中同时在同一个套接字上读写

c - 为什么我不能传入变量来实现交换功能?

c++ - 从 boost::function 中提取参数

c++ - 从 boost asio 客户端发送数据

c++ - 使用 boost 遍历 mmaped gzip 文件

c++ - 编译一个相当简单的c++ 11程序时gcc和clang之间的不同结果

c++ - FireBreath 和 ICE38

c++ - 将文本文件中的数据提取到结构中

c++ - 按字母顺序排列结构数组的问题。从 Z 到 A 有效,但不能从 A 到 Z

javascript子函数将返回值传递给父函数返回值