简体   繁体   English

使用std :: map错误函数调用的boost :: factory

[英]boost::factory with std::map bad function call

I'm having a problem with understanding how boost::factory does its job. 我在了解boost :: factory如何完成工作时遇到问题。 The following code will throw in the ctor of test2. 以下代码将放入test2的ctor中。

#include <boost/functional/factory.hpp>
#include <boost/function.hpp>

typedef boost::function<A*()> creator;
typedef std::map<string,creator> factory;

class A{
}

class AA : A {
}

class AB : A {
}

class C{
public: 
 C(factory& f);

 factory _f;
}

int main(){
 factory f;
 f["1"] = boost::factory<AA*>();
 f["2"] = boost::factory<AB*>();

 C test(f);
 C test2(f);
}

C::C(factory& f){
  _f = f;
  A* t = _f["1"]();
}

The message is 消息是

terminate called after throwing an instance of 'boost::exception_detail::clone_impl 抛出'boost :: exception_detail :: clone_impl实例后调用终止

' what(): call to empty boost::function 'what():调用空的boost :: function

I think I don't understand copy/move behaviour here and that is causing the problem. 我认为我不了解此处的复制/移动行为,这是导致问题的原因。 As far as I understand, the factory gets copied in C::C, so every call to _f[something]() should get its own function to call. 据我了解,工厂是在C :: C中复制的,因此对_f[something]()每次调用都应具有自己的函数来调用。 But somehow the function is moved out of the factory in the ctor of test, and then I get a bad function call, cause f["1"] is left in an undefined state. 但是以某种方式将函数在测试的ctor中移出了工厂,然后我得到了一个错误的函数调用,原因是f [“ 1”]处于未定义状态。 Please help. 请帮忙。

As far as I understand, the factory gets copied in C::C 据我了解,工厂被复制到C :: C

Yes. 是。

But somehow the function is moved out of the factory in the ctor of test 但是以某种方式将功能从测试中心移出了工厂

Nope. 不。 If that seems to be the case you likely have Undefined Behaviour elsewhere. 如果真是这样,您可能在其他地方有未定义行为 Or you might be running a different (buggy) version of boost (this seems highly unlikely). 或者您可能正在运行其他(越野车)版本的boost(这似乎不太可能)。

See if you can reproduce it with the code shown here: 看看是否可以使用此处显示的代码来重现它:

Live On Coliru 生活在Coliru

#include <boost/functional/factory.hpp>
#include <boost/function.hpp>
#include <map>
#include <iostream>

struct A {};
struct AA : A {
    AA() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};
struct AB : A {
    AB() { std::cout << __PRETTY_FUNCTION__ << "\n"; }
};

typedef boost::function<A*()> creator;
typedef std::map<std::string, creator> factory;

struct C {
    C(factory &f){
        _f = f;
        for (auto& e : _f)
            std::cout << "entry for " << e.first << " present " << (!!e.second) << "\n";
    }
    factory _f;
};

int main() {
    factory f;
    f["1"] = boost::factory<AA*>();
    f["2"] = boost::factory<AB*>();

    C test(f);
    delete f["1"]();
    delete f["2"]();
    delete f["1"]();
    delete f["2"]();

    C test2(f);
    delete f["1"]();
    delete f["2"]();
    delete f["1"]();
    delete f["2"]();
}

Prints 打印

entry for 1 present 1
entry for 2 present 1
AA::AA()
AB::AB()
AA::AA()
AB::AB()
entry for 1 present 1
entry for 2 present 1
AA::AA()
AB::AB()
AA::AA()
AB::AB()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM