需要为 on_exit 事件设置句柄函数。
像在 boost 教程中一样尝试分配。
#include <boost/process.hpp>
#include <iostream>
using namespace boost::process;
using err_code = boost::system::error_code;
int main() {
child proc("ls -l", boost::process::on_exit = [](int exit, const err_code& ec_in){std::cout << "hah" << std::endl;});
proc.wait();
int ret = proc.exit_code();
return 0;
}
g++ test.cpp -o 测试 -lboost_thread -lboost_system -pthread
test-proc.cpp: In function ‘int main()’:
test-proc.cpp:6:119: error: no match for ‘operator=’ (operand types are ‘const boost::process::detail::on_exit_’ and ‘main()::<lambda(int, const err_code&)>’)
child proc("ls -l", boost::process::on_exit = [](int exit, const err_code& ec_in){std::cout << "hah" << std::endl;});
^
In file included from /usr/include/boost/process/async.hpp:33:0,
from /usr/include/boost/process.hpp:23,
from test-proc.cpp:1:
/usr/include/boost/process/detail/on_exit.hpp:40:19: note: candidate: boost::process::detail::posix::on_exit_ boost::process::detail::on_exit_::operator=(const std::function<void(int, const std::error_code&)>&) const
api::on_exit_ operator= (const std::function<void(int, const std::error_code&)> & f) const {return f;}
^~~~~~~~
/usr/include/boost/process/detail/on_exit.hpp:40:19: note: no known conversion for argument 1 from ‘main()::<lambda(int, const err_code&)>’ to ‘const std::function<void(int, const std::error_code&)>&’
/usr/include/boost/process/detail/on_exit.hpp:43:19: note: candidate: boost::process::detail::posix::on_exit_ boost::process::detail::on_exit_::operator=(std::future<int>&) const
api::on_exit_ operator= (std::future<int> &f) const {return on_exit_from_future(f);}
^~~~~~~~
/usr/include/boost/process/detail/on_exit.hpp:43:19: note: no known conversion for argument 1 from ‘main()::<lambda(int, const err_code&)>’ to ‘std::future<int>&’
/usr/include/boost/process/detail/on_exit.hpp:38:8: note: candidate: constexpr boost::process::detail::on_exit_& boost::process::detail::on_exit_::operator=(const boost::process::detail::on_exit_&)
struct on_exit_
^~~~~~~~
/usr/include/boost/process/detail/on_exit.hpp:38:8: note: no known conversion for argument 1 from ‘main()::<lambda(int, const err_code&)>’ to ‘const boost::process::detail::on_exit_&’
/usr/include/boost/process/detail/on_exit.hpp:38:8: note: candidate: constexpr boost::process::detail::on_exit_& boost::process::detail::on_exit_::operator=(boost::process::detail::on_exit_&&)
/usr/include/boost/process/detail/on_exit.hpp:38:8: note: no known conversion for argument 1 from ‘main()::<lambda(int, const err_code&)>’ to ‘boost::process::detail::on_exit_&&’