Function get_environment_variable


#include "breeze/environment/get_environment_variable.hpp"

maybe< std::string > get_environment_variable(std::string const &name)


Searches an implementation-defined list, as if by calling std::getenv(), for a string that matches name and returns the associated value, if any.

We emphasize that the 'as if' above is part of this function's contract.

This function overcomes three usability issues of std::getenv():

  • const unsafety: getenv() returns a pointer to (non-const) char (which the program shall not modify)
  • subsequent calls to getenv() may overwrite the pointed to string
  • if the searched to string is not found getenv() returns a null pointer; that can easily lead to undefined behavior, if that value is passed to one of the constructors of std::string which take a char const *
The value associated to the given name, or an invalid maybe if there is none.
The behavior on passing an empty string for the name parameter is implementation-defined: please check the documentation provided with your C++ implementation.


Line 56 in breeze/environment/get_environment_variable.hpp.