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