...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
boost::process::v2::process_environment — Initializer for the environment of sub process.
// In header: <boost/process/v2/environment.hpp> struct process_environment { // construct/copy/destruct process_environment(std::initializer_list< string_view >); template<typename Args> process_environment(Args &&); // public static functions template<typename Args> static std::vector< const char * > build_env(Args &&, typename std::enable_if< std::is_convertible< decltype(*std::begin(std::declval< Args >())), cstring_ref >::value, ::type * = nullptr); // public member functions template<typename Args> std::vector< const char * > build_env(Args &&, typename std::enable_if< !std::is_convertible< decltype(*std::begin(std::declval< Args >())), cstring_ref >::value, ::type * = nullptr); error_code on_setup(posix::default_launcher &, const filesystem::path &, const char *const *); // public data members std::vector< environment::key_value_pair > env_buffer; std::vector< const char * > env; };
This will set the environment in a subprocess:
process proc{executor, find_executable("printenv"), {"foo"}, process_environment{"foo=bar"}};
The environment initializer will persist it's state, so that it can be used multiple times. Do however note the the Operating System is allowed to modify the internal state.
auto exe = find_executable("printenv"); process_environment env = {"FOO=BAR", "BAR=FOO"}; process proc1(executor, exe, {"FOO"}, env); process proc2(executor, exe, {"BAR"}, env);
process_environment
public static functionstemplate<typename Args> static std::vector< const char * > build_env(Args && args, typename std::enable_if< std::is_convertible< decltype(*std::begin(std::declval< Args >())), cstring_ref >::value, ::type * = nullptr);
process_environment
public member functionstemplate<typename Args> std::vector< const char * > build_env(Args && args, typename std::enable_if< !std::is_convertible< decltype(*std::begin(std::declval< Args >())), cstring_ref >::value, ::type * = nullptr);
error_code on_setup(posix::default_launcher & launcher, const filesystem::path &, const char *const *);