如何初始化boost :: process :: child(boost process 0.5)?

时间:2014-11-12 06:05:59

标签: c++ boost

我想存储boost :: process的子节点,但不知道如何初始化它

操作系统:win7 64位 编译器:msvc2008 32位 提升:1_55_0

简化后的例子

#include <boost/process/initializers.hpp>
#include <boost/process.hpp>

#include <boost/system/system_error.hpp>

#include <iostream>

void test_boost_system()
{
    namespace bp = boost::process;
    namespace bpi = boost::process::initializers;

    //bp::child child; //#1

    boost::system::error_code ec;
    bp::child child_2 = bp::execute(bpi::run_exe("ldapInterface.exe"), bpi::set_on_error(ec));
    if(ec.value() != 0){
        std::cout<<ec.message()<<std::endl;
    }else{
        std::cout<<"success"<<std::endl;
    }    
}

如果我不想使用execute来初始化它,我怎么能初始化它?

伪代码:

namespace bp = boost::process;
namespace bpi = boost::process::initializers; 

class process_manager
{
public:
    ~process_manager() { bp::terminate(child_); }

    void open_process(std::string const &process)
    {

       child_ = bp::execute(bpi::run_exe(process)); //compile error
    }

private:
    bp::child child_;
};

错误讯息: 错误C2512:&#39; boost :: process :: windows :: child&#39; :没有合适的默认构造函数

1 个答案:

答案 0 :(得分:5)

使用一个允许你懒惰地初始化的包装器。

E.g。

class process_manager
{
public:
    ~process_manager() { if (child_) bp::terminate(*child_); }

    void open_process(std::string const &process)
    {
        child_ = bp::execute(bpi::run_exe(process)); //compile error
    }

private:
    boost::optional<bp::child> child_;
};